
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Java_Summary · GitBook笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="chenjiebin">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-prism/prism.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="java-jvm.html" />
    
    
    <link rel="prev" href="java.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../document/kubernetes/kubernetes.html">
            
                <a href="../document/kubernetes/kubernetes.html">
            
                    
                    Kubernetes英文教程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../document/kubernetes/kubernetes.html">
            
                <a href="../document/kubernetes/kubernetes.html#concepts">
            
                    
                    Kubernetes Concepts
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.1" data-path="../document/kubernetes/concepts/overview.html">
            
                <a href="../document/kubernetes/concepts/overview.html">
            
                    
                    Kubernetes Overview
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.2" data-path="../document/kubernetes/concepts/components.html">
            
                <a href="../document/kubernetes/concepts/components.html">
            
                    
                    Kubernetes Components
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.3" data-path="../document/kubernetes/concepts/nodes.html">
            
                <a href="../document/kubernetes/concepts/nodes.html">
            
                    
                    Kubernetes Nodes
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../document/kubernetes/kubernetes.html">
            
                <a href="../document/kubernetes/kubernetes.html#tutorial">
            
                    
                    Kubernetes Tutorial
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../document/kubernetes/tutorial/kubernetes-basics.html">
            
                <a href="../document/kubernetes/tutorial/kubernetes-basics.html">
            
                    
                    Kubernetes Basics
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="java.html">
            
                <a href="java.html">
            
                    
                    Java基础知识
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.3.1" data-path="java-summary.html">
            
                <a href="java-summary.html">
            
                    
                    Java_Summary
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="java-jvm.html">
            
                <a href="java-jvm.html">
            
                    
                    Java_JVM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="effectiveJava.html">
            
                <a href="effectiveJava.html">
            
                    
                    effectiveJava
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="design-pattern.html">
            
                <a href="design-pattern.html">
            
                    
                    设计模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="JavaTest.html">
            
                <a href="JavaTest.html">
            
                    
                    JavaTest
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="myframe.html">
            
                <a href="myframe.html">
            
                    
                    MyFrame
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../database/database.html">
            
                <a href="../database/database.html">
            
                    
                    数据库基础知识
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../database/mysql.html">
            
                <a href="../database/mysql.html">
            
                    
                    MySQL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../database/redis.html">
            
                <a href="../database/redis.html">
            
                    
                    Redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../database/kingbase.html">
            
                <a href="../database/kingbase.html">
            
                    
                    KingBase
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../database/polardb.html">
            
                <a href="../database/polardb.html">
            
                    
                    PolarDB
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../system/system.html">
            
                <a href="../system/system.html">
            
                    
                    环境配置工具
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../system/nodejs.html">
            
                <a href="../system/nodejs.html">
            
                    
                    Node.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../system/gitbook.html">
            
                <a href="../system/gitbook.html">
            
                    
                    Gitbook
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../system/linux.html">
            
                <a href="../system/linux.html">
            
                    
                    Linux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../system/kylin.html">
            
                <a href="../system/kylin.html">
            
                    
                    麒麟系统
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../tool/tool.html">
            
                <a href="../tool/tool.html">
            
                    
                    开发工具介绍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../tool/docker.html">
            
                <a href="../tool/docker.html">
            
                    
                    Docker
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../tool/idea.html">
            
                <a href="../tool/idea.html">
            
                    
                    Idea
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../tool/maven.html">
            
                <a href="../tool/maven.html">
            
                    
                    Maven
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../tool/gradle.html">
            
                <a href="../tool/gradle.html">
            
                    
                    Gradle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../framework/kubernate.html">
            
                <a href="../framework/kubernate.html">
            
                    
                    Kubernetes
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Java_Summary</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#java&#x77E5;&#x8BC6;&#x6C47;&#x603B;"><b>1. </b>Java&#x77E5;&#x8BC6;&#x6C47;&#x603B;</a></li><ul><li><span class="title-icon "></span><a href="#jdkjrejvm"><b>1.1. </b>jdk/jre/jvm</a></li><li><span class="title-icon "></span><a href="#oracle-jdk-&#x548C;-openjdk"><b>1.2. </b>Oracle jdk &#x548C; OpenJDK</a></li><li><span class="title-icon "></span><a href="#java&#x548C;c"><b>1.3. </b>Java&#x548C;C++</a></li><li><span class="title-icon "></span><a href="#&#x5B57;&#x7B26;&#x578B;&#x5E38;&#x91CF;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;"><b>1.4. </b>&#x5B57;&#x7B26;&#x578B;&#x5E38;&#x91CF;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;</a></li><li><span class="title-icon "></span><a href="#&#x57FA;&#x672C;&#x7C7B;&#x578B;&#x7F13;&#x51B2;&#x6C60;"><b>1.5. </b>&#x57FA;&#x672C;&#x7C7B;&#x578B;&#x7F13;&#x51B2;&#x6C60;</a></li><li><span class="title-icon "></span><a href="#string"><b>1.6. </b>String</a></li><ul><li><span class="title-icon "></span><a href="#&#x4E0D;&#x53EF;&#x53D8;&#x7684;&#x597D;&#x5904;"><b>1.6.1. </b>&#x4E0D;&#x53EF;&#x53D8;&#x7684;&#x597D;&#x5904;</a></li><li><span class="title-icon "></span><a href="#string-stringbuffer-stringbuilder"><b>1.6.2. </b>String StringBuffer StringBuilder</a></li></ul><li><span class="title-icon "></span><a href="#&#x6807;&#x8BC6;&#x7B26;&#x548C;&#x5173;&#x952E;&#x5B57;"><b>1.7. </b>&#x6807;&#x8BC6;&#x7B26;&#x548C;&#x5173;&#x952E;&#x5B57;</a></li><li><span class="title-icon "></span><a href="#&#x81EA;&#x589E;&#x81EA;&#x51CF;&#x8FD0;&#x7B97;&#x7B26;"><b>1.8. </b>&#x81EA;&#x589E;&#x81EA;&#x51CF;&#x8FD0;&#x7B97;&#x7B26;</a></li><li><span class="title-icon "></span><a href="#continue-break-return"><b>1.9. </b>continue break return</a></li><li><span class="title-icon "></span><a href="#java&#x6CDB;&#x578B;-&#x7C7B;&#x578B;&#x64E6;&#x9664;-&#x901A;&#x914D;&#x7B26;"><b>1.10. </b>Java&#x6CDB;&#x578B; &#x7C7B;&#x578B;&#x64E6;&#x9664; &#x901A;&#x914D;&#x7B26;</a></li><ul><li><span class="title-icon "></span><a href="#java&#x6CDB;&#x578B;"><b>1.10.1. </b>Java&#x6CDB;&#x578B;</a></li><li><span class="title-icon "></span><a href="#&#x901A;&#x914D;&#x7B26;"><b>1.10.2. </b>&#x901A;&#x914D;&#x7B26;</a></li><li><span class="title-icon "></span><a href="#&#x7C7B;&#x578B;&#x64E6;&#x9664;"><b>1.10.3. </b>&#x7C7B;&#x578B;&#x64E6;&#x9664;</a></li></ul><li><span class="title-icon "></span><a href="#this"><b>1.11. </b>this</a></li><li><span class="title-icon "></span><a href="#lambda"><b>1.12. </b>Lambda</a></li><li><span class="title-icon "></span><a href="#object"><b>1.13. </b>Object</a></li><ul><li><span class="title-icon "></span><a href="#equals"><b>1.13.1. </b>equals()</a></li><li><span class="title-icon "></span><a href="#hashcode"><b>1.13.2. </b>hashcode()</a></li><li><span class="title-icon "></span><a href="#tostring"><b>1.13.3. </b>toString()</a></li><li><span class="title-icon "></span><a href="#clone"><b>1.13.4. </b>clone()</a></li></ul></ul><li><span class="title-icon "></span><a href="#&#x591A;&#x7EBF;&#x7A0B;"><b>2. </b>&#x591A;&#x7EBF;&#x7A0B;</a></li><ul><li><span class="title-icon "></span><a href="#&#x5185;&#x5B58;&#x6A21;&#x578B;"><b>2.1. </b>&#x5185;&#x5B58;&#x6A21;&#x578B;</a></li><li><span class="title-icon "></span><a href="#&#x8FDB;&#x7A0B;&#x4E0E;&#x7EBF;&#x7A0B;"><b>2.2. </b>&#x8FDB;&#x7A0B;&#x4E0E;&#x7EBF;&#x7A0B;</a></li><li><span class="title-icon "></span><a href="#&#x540C;&#x6B65;&#x4E0E;&#x5F02;&#x6B65;"><b>2.3. </b>&#x540C;&#x6B65;&#x4E0E;&#x5F02;&#x6B65;</a></li><li><span class="title-icon "></span><a href="#&#x5E76;&#x53D1;&#x548C;&#x5E76;&#x884C;"><b>2.4. </b>&#x5E76;&#x53D1;&#x548C;&#x5E76;&#x884C;</a></li><li><span class="title-icon "></span><a href="#&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;"><b>2.5. </b>&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;</a></li><li><span class="title-icon "></span><a href="#&#x7EBF;&#x7A0B;&#x5B89;&#x5168;"><b>2.6. </b>&#x7EBF;&#x7A0B;&#x5B89;&#x5168;</a></li><li><span class="title-icon "></span><a href="#synchronized"><b>2.7. </b>synchronized</a></li><li><span class="title-icon "></span><a href="#volatile"><b>2.8. </b>volatile</a></li><li><span class="title-icon "></span><a href="#condition"><b>2.9. </b>Condition</a></li><li><span class="title-icon "></span><a href="#&#x7EBF;&#x7A0B;&#x901A;&#x77E5;&#x7B49;&#x5F85;"><b>2.10. </b>&#x7EBF;&#x7A0B;&#x901A;&#x77E5;&#x7B49;&#x5F85;</a></li><li><span class="title-icon "></span><a href="#lock"><b>2.11. </b>Lock</a></li><li><span class="title-icon "></span><a href="#reentrantlock"><b>2.12. </b>ReentrantLock</a></li><li><span class="title-icon "></span><a href="#reentrantreadwritelock"><b>2.13. </b>ReentrantReadWriteLock</a></li><li><span class="title-icon "></span><a href="#countdownlatch"><b>2.14. </b>CountDownLatch</a></li><li><span class="title-icon "></span><a href="#cyclicbarrier"><b>2.15. </b>CyclicBarrier</a></li><li><span class="title-icon "></span><a href="#&#x7EBF;&#x7A0B;&#x6C60;"><b>2.16. </b>&#x7EBF;&#x7A0B;&#x6C60;</a></li><li><span class="title-icon "></span><a href="#threadlocal"><b>2.17. </b>ThreadLocal</a></li></ul><li><span class="title-icon "></span><a href="#thread"><b>3. </b>Thread</a></li><ul><li><span class="title-icon "></span><a href="#thread&#x542F;&#x52A8;&#x539F;&#x7406;"><b>3.1. </b>Thread&#x542F;&#x52A8;&#x539F;&#x7406;</a></li><ul><li><span class="title-icon "></span><a href="#start&#x65B9;&#x6CD5;"><b>3.1.1. </b>start()&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#start0&#x672C;&#x5730;&#x65B9;&#x6CD5;"><b>3.1.2. </b>start0()&#x672C;&#x5730;&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#jvm&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;"><b>3.1.3. </b>JVM&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;</a></li><li><span class="title-icon "></span><a href="#jvm&#x542F;&#x52A8;&#x7EBF;&#x7A0B;"><b>3.1.4. </b>JVM&#x542F;&#x52A8;&#x7EBF;&#x7A0B;</a></li><li><span class="title-icon "></span><a href="#jvm&#x56DE;&#x8C03;"><b>3.1.5. </b>JVM&#x56DE;&#x8C03;</a></li><li><span class="title-icon "></span><a href="#&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x603B;&#x7ED3;"><b>3.1.6. </b>&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x603B;&#x7ED3;</a></li><li><span class="title-icon "></span><a href="#&#x65B9;&#x6CD5;&#x5F15;&#x7528;"><b>3.1.7. </b>&#x65B9;&#x6CD5;&#x5F15;&#x7528;</a></li></ul></ul></ul></div><a href="#java&#x77E5;&#x8BC6;&#x6C47;&#x603B;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="java&#x77E5;&#x8BC6;&#x6C47;&#x603B;"><a name="java&#x77E5;&#x8BC6;&#x6C47;&#x603B;" class="anchor-navigation-ex-anchor" href="#java&#x77E5;&#x8BC6;&#x6C47;&#x603B;"><i class="fa fa-link" aria-hidden="true"></i></a>1. Java&#x77E5;&#x8BC6;&#x6C47;&#x603B;</h1>
<h2 id="jdkjrejvm"><a name="jdkjrejvm" class="anchor-navigation-ex-anchor" href="#jdkjrejvm"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. jdk/jre/jvm</h2>
<p>JDK&#xFF1A;Java Development Kit &#x7684;&#x7B80;&#x79F0;&#xFF0C;Java&#x5F00;&#x53D1;&#x5DE5;&#x5177;&#x5305;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;Java&#x7684;&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x548C;&#x8FD0;&#x884C;&#x73AF;&#x5883;</p>
<p>JRE&#xFF1A;Java Runtime Enviroment&#x7684;&#x7B80;&#x79F0;&#xFF0C;Java&#x8FD0;&#x884C;&#x73AF;&#x5883;&#xFF0C;&#x4E3A;Java&#x7684;&#x8FD0;&#x884C;&#x63D0;&#x4F9B;&#x4E86;&#x6240;&#x9700;&#x73AF;&#x5883;</p>
<p>JVM: &#x662F;&#x8FD0;&#x884C;Java&#x5B57;&#x8282;&#x7801;&#x7684;&#x865A;&#x62DF;&#x673A;&#xFF0C;&#x9488;&#x5BF9;&#x4E0D;&#x540C;&#x7CFB;&#x7EDF;&#x7684;&#x7279;&#x5B9A;&#x5B9E;&#x73B0;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x76F8;&#x540C;&#x7684;&#x5B57;&#x8282;&#x7801;&#x5F97;&#x5230;&#x76F8;&#x540C;&#x7684;&#x7ED3;&#x679C;</p>
<p>JDK&#x5305;&#x542B;JRE&#xFF0C;&#x540C;&#x65F6;&#x5305;&#x542B;&#x4E86;&#x7F16;&#x8BD1;&#x7684;Java&#x6E90;&#x7801;&#x7684;&#x7F16;&#x8BD1;&#x5668;javac&#xFF0C;Java&#x7A0B;&#x5E8F;&#x8C03;&#x8BD5;&#x548C;&#x5206;&#x6790;&#x7684;&#x5DE5;&#x5177;</p>
<p>&#x7B80;&#x5355;&#x8BF4;&#xFF1A;&#x53EA;&#x8FD0;&#x884C;Java&#x73AF;&#x5883;&#xFF0C;&#x53EA;&#x9700;&#x5B89;&#x88C5;JRE&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x9700;&#x8981;&#x7F16;&#x5199;Java&#x7F16;&#x7A0B;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x5B89;&#x88C5;JDK</p>
<h2 id="oracle-jdk-&#x548C;-openjdk"><a name="oracle-jdk-&#x548C;-openjdk" class="anchor-navigation-ex-anchor" href="#oracle-jdk-&#x548C;-openjdk"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. Oracle jdk &#x548C; OpenJDK</h2>
<p>OpenJDK&#x662F;&#x4E00;&#x4E2A;&#x53C2;&#x8003;&#x6A21;&#x578B;&#x5E76;&#x4E14;&#x662F;&#x5B8C;&#x5168;&#x5F00;&#x6E90;&#x7684;&#xFF0C;OracleJdk&#x662F;OpenJDK&#x7684;&#x4E00;&#x4E2A;&#x5B9E;&#x73B0;&#xFF0C;&#x90E8;&#x5206;&#x5F00;&#x6E90;</p>
<p>OracleJDK&#x6BD4;OpenJDK&#x66F4;&#x52A0;&#x7A33;&#x5B9A;&#xFF0C;Oracle JDK&#x6709;&#x66F4;&#x591A;&#x7684;&#x7C7B;&#x548C;&#x4E00;&#x4E9B;&#x9519;&#x8BEF;&#x4FEE;&#x590D;</p>
<p>Oracle JDK&#x6BD4;OpenJDK&#x63D0;&#x4F9B;&#x4E86;&#x54CD;&#x5E94;&#x6027;&#x548C;JVM&#x6027;&#x80FD;&#x65B9;&#x9762;&#x66F4;&#x597D;&#x7684;&#x6027;&#x80FD;</p>
<p>Oracle JDK&#x4E0D;&#x4F1A;&#x4E3A;&#x5373;&#x5C06;&#x53D1;&#x5E03;&#x7684;&#x7248;&#x672C;&#x63D0;&#x4F9B;&#x957F;&#x671F;&#x652F;&#x6301;</p>
<p>Oracle JDK&#x4F7F;&#x7528;BCL/OTN&#x534F;&#x8BAE;&#x83B7;&#x5F97;&#x8BB8;&#x53EF;&#xFF0C;&#x800C;OpenJDK&#x6839;&#x636E;GPL v2&#x8BB8;&#x53EF;&#x83B7;&#x5F97;&#x8BB8;&#x53EF;</p>
<p>&#x62D3;&#x5C55;&#xFF1A;</p>
<ul>
<li>BCL&#x534F;&#x8BAE;&#xFF08;Oracle Binary Code License Agreement&#xFF09;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;JDK&#xFF08;&#x652F;&#x6301;&#x5546;&#x7528;&#xFF09;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x80FD;&#x8FDB;&#x884C;&#x4FEE;&#x6539;</li>
<li>OTN&#x534F;&#x8BAE;&#xFF08;Oracle Technology Network License Agreement&#xFF09;11&#x53CA;&#x4E4B;&#x540E;&#x65B0;&#x53D1;&#x5E03;&#x7684;JDK&#x7528;&#x7684;&#x90FD;&#x662F;&#x8FD9;&#x4E2A;&#x534F;&#x8BAE;&#xFF0C;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x79C1;&#x4E0B;&#x7528;&#xFF0C;&#x4F46;&#x662F;&#x5546;&#x7528;&#x9700;&#x8981;&#x4ED8;&#x8D39;</li>
</ul>
<h2 id="java&#x548C;c"><a name="java&#x548C;c" class="anchor-navigation-ex-anchor" href="#java&#x548C;c"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. Java&#x548C;C++</h2>
<p>&#x90FD;&#x662F;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#xFF0C;&#x652F;&#x6301;&#x5C01;&#x88C5;&#x3001;&#x7EE7;&#x627F;&#x548C;&#x591A;&#x6001;</p>
<p>Java&#x4E0D;&#x63D0;&#x4F9B;&#x6307;&#x9488;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#x5185;&#x5B58;&#xFF0C;&#x7A0B;&#x5E8F;&#x5185;&#x5B58;&#x66F4;&#x52A0;&#x5B89;&#x5168;</p>
<p>Java&#x7684;&#x7C7B;&#x662F;&#x5355;&#x7EE7;&#x627F;&#xFF0C;&#x63A5;&#x53E3;&#x53EF;&#x4EE5;&#x591A;&#x7EE7;&#x627F;&#xFF0C;C++&#x652F;&#x6301;&#x591A;&#x91CD;&#x7EE7;&#x627F;</p>
<p>Java&#x6709;&#x81EA;&#x52A8;&#x5185;&#x5B58;&#x7BA1;&#x7406;&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;GC&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x7A0B;&#x5E8F;&#x5458;&#x624B;&#x52A8;&#x91CA;&#x653E;&#x65E0;&#x7528;&#x5185;&#x5B58;</p>
<p>C++&#x540C;&#x65F6;&#x652F;&#x6301;&#x65B9;&#x6CD5;&#x91CD;&#x8F7D;&#x548C;&#x64CD;&#x4F5C;&#x7B26;&#x91CD;&#x8F7D;&#xFF0C;&#x4F46;&#x662F;Java&#x53EA;&#x652F;&#x6301;&#x65B9;&#x6CD5;&#x91CD;&#x8F7D;&#xFF08;&#x64CD;&#x4F5C;&#x7B26;&#x91CD;&#x8F7D;&#x589E;&#x52A0;&#x4E86;&#x590D;&#x6742;&#x6027;&#xFF09;</p>
<h2 id="&#x5B57;&#x7B26;&#x578B;&#x5E38;&#x91CF;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;"><a name="&#x5B57;&#x7B26;&#x578B;&#x5E38;&#x91CF;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;" class="anchor-navigation-ex-anchor" href="#&#x5B57;&#x7B26;&#x578B;&#x5E38;&#x91CF;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4. &#x5B57;&#x7B26;&#x578B;&#x5E38;&#x91CF;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;</h2>
<p>1.&#x5F62;&#x5F0F;&#xFF1A;&#x5B57;&#x7B26;&#x5E38;&#x91CF;&#x662F;&#x5355;&#x5F15;&#x53F7;&#x5F15;&#x8D77;&#x7684;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;&#x662F;&#x53CC;&#x5F15;&#x53F7;&#x5F15;&#x8D77;&#x7684;0&#x5404;&#x6216;&#x82E5;&#x5E72;&#x4E2A;&#x5B57;&#x7B26;</p>
<p>2.&#x542B;&#x4E49;&#xFF1A;&#x5B57;&#x7B26;&#x5E38;&#x91CF;&#x76F8;&#x5F53;&#x4E8E;&#x4E00;&#x4E2A;&#x6574;&#x578B;&#x503C;&#xFF08;ASCII&#x503C;&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x53C2;&#x52A0;&#x8868;&#x8FBE;&#x5F0F;&#x8FD0;&#x7B97;&#xFF1B;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;&#x5730;&#x5740;&#x503C;&#xFF08;&#x8BE5;&#x5B57;&#x7B26;&#x4E32;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x5B58;&#x653E;&#x4F4D;&#x7F6E;&#xFF09;</p>
<p>3.&#x5185;&#x5B58;&#x5927;&#x5C0F;&#xFF1A;&#x5B57;&#x7B26;&#x5E38;&#x91CF;&#x53EA;&#x5360;2&#x5404;&#x5B57;&#x8282;&#xFF1B;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;&#x5360;&#x82E5;&#x5E72;&#x4E2A;&#x5B57;&#x8282;&#xFF08;char&#x5728;Java&#x4E2D;&#x5360;&#x4E24;&#x4E2A;&#x5B57;&#x8282;&#xFF09;</p>
<table>
<thead>
<tr>
<th>&#x57FA;&#x672C;&#x7C7B;&#x578B;</th>
<th>&#x5927;&#x5C0F;</th>
<th>&#x5305;&#x88C5;&#x5668;&#x7C7B;&#x578B;</th>
<th>&#x6700;&#x5C0F;&#x503C;</th>
<th>&#x6700;&#x5927;&#x503C;</th>
</tr>
</thead>
<tbody>
<tr>
<td>boolean</td>
<td>-</td>
<td>Boolean</td>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td>byte</td>
<td>8 bits</td>
<td>Byte</td>
<td>-128</td>
<td>+127</td>
</tr>
<tr>
<td>char</td>
<td>16 bits</td>
<td>Character</td>
<td>Unicode 0</td>
<td>Unicode 2^16-1</td>
</tr>
<tr>
<td>short</td>
<td>16 bits</td>
<td>Short</td>
<td>-2^15</td>
<td>+2^15-1</td>
</tr>
<tr>
<td>int</td>
<td>32 bits</td>
<td>Integer</td>
<td>-2^31</td>
<td>+2^31-1</td>
</tr>
<tr>
<td>float</td>
<td>32 bits</td>
<td>Float</td>
<td></td>
<td></td>
</tr>
<tr>
<td>long</td>
<td>64 bits</td>
<td>Long</td>
<td>-2^63</td>
<td>+2^63-1</td>
</tr>
<tr>
<td>double</td>
<td>64 bits</td>
<td>Double</td>
<td></td>
<td></td>
</tr>
<tr>
<td>void</td>
<td>-</td>
<td>Void</td>
<td>-</td>
<td>-</td>
</tr>
</tbody>
</table>
<p>1&#x5B57;&#x8282; 8&#x4F4D;&#xFF1A;byte</p>
<p>2&#x5B57;&#x8282; 16&#x4F4D;&#xFF1A;char short</p>
<p>4&#x5B57;&#x8282; 32&#x4F4D;&#xFF1A;int float</p>
<p>8&#x5B57;&#x8282; 64&#x4F4D;&#xFF1A;long double</p>
<h2 id="&#x57FA;&#x672C;&#x7C7B;&#x578B;&#x7F13;&#x51B2;&#x6C60;"><a name="&#x57FA;&#x672C;&#x7C7B;&#x578B;&#x7F13;&#x51B2;&#x6C60;" class="anchor-navigation-ex-anchor" href="#&#x57FA;&#x672C;&#x7C7B;&#x578B;&#x7F13;&#x51B2;&#x6C60;"><i class="fa fa-link" aria-hidden="true"></i></a>1.5. &#x57FA;&#x672C;&#x7C7B;&#x578B;&#x7F13;&#x51B2;&#x6C60;</h2>
<p>Byte Short Integer Long 4&#x79CD;&#x5305;&#x88C5;&#x7C7B;&#x578B;&#x9ED8;&#x8BA4;&#x521B;&#x5EFA;&#x4E86;&#x6570;&#x503C; [-128, 127] &#x7684;&#x76F8;&#x5E94;&#x7C7B;&#x578B;&#x7684;&#x7F13;&#x5B58;&#x6570;&#x503C;</p>
<p>Character &#x521B;&#x5EFA;&#x4E86;&#x6570;&#x503C;&#x5728; [0, 127] &#x8303;&#x56F4;&#x7684;&#x7F13;&#x5B58;&#x6570;&#x636E;</p>
<p>Float Double &#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x5E38;&#x91CF;&#x6C60;&#x6280;&#x672F;</p>
<pre class="language-"><code>boolean: true false
byte: all values
short: -128~127
int: -128~127
char: \u0000~\u007F
</code></pre><p>Integer.valueOf&#x4F1A;&#x4F7F;&#x7528;&#x7F13;&#x51B2;&#x6C60;&#x4E2D;&#x7684;&#x5BF9;&#x8C61;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Integer</span> <span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// &#x5728;&#x7F13;&#x5B58;&#x6C60;&#xFF08;-128~127&#xFF09;&#x4E2D;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">&gt;=</span><span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>low<span class="token operator">&amp;&amp;</span>i<span class="token operator">&lt;=</span><span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>high<span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>cache<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>low<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
</code></pre>
<h2 id="string"><a name="string" class="anchor-navigation-ex-anchor" href="#string"><i class="fa fa-link" aria-hidden="true"></i></a>1.6. String</h2>
<p>String&#x88AB;&#x7533;&#x660E;&#x4E3A;final&#xFF0C;&#x56E0;&#x6B64;&#x4E0D;&#x53EF;&#x88AB;&#x7EE7;&#x627F;&#xFF08;Integer&#x7B49;&#x5305;&#x88C5;&#x7C7B;&#x4E5F;&#x4E0D;&#x80FD;&#x88AB;&#x7EE7;&#x627F;&#xFF09;&#xFF0C;&#x662F;&#x4E0D;&#x53EF;&#x53D8;&#x7684;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">// java8</span>
<span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">String</span>
        <span class="token keyword">implements</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span>Serializable</span><span class="token punctuation">,</span> <span class="token class-name">Comparable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">,</span> <span class="token class-name">CharSequence</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">char</span> value<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// &#x4F7F;&#x7528;char&#x6570;&#x7EC4;</span>
<span class="token punctuation">}</span>

<span class="token comment">// java9</span>
<span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">String</span>
        <span class="token keyword">implements</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span>Serializable</span><span class="token punctuation">,</span> <span class="token class-name">Comparable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">,</span> <span class="token class-name">CharSequence</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> value<span class="token punctuation">;</span> <span class="token comment">// &#x4F7F;&#x7528;byte&#x6570;&#x7EC4;</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">byte</span> coder<span class="token punctuation">;</span> <span class="token comment">// &#x5E76;&#x4E14;&#x6807;&#x660E;&#x4F7F;&#x7528;&#x7684;&#x7F16;&#x7801;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x4E0D;&#x53EF;&#x53D8;&#x7684;&#x597D;&#x5904;"><a name="&#x4E0D;&#x53EF;&#x53D8;&#x7684;&#x597D;&#x5904;" class="anchor-navigation-ex-anchor" href="#&#x4E0D;&#x53EF;&#x53D8;&#x7684;&#x597D;&#x5904;"><i class="fa fa-link" aria-hidden="true"></i></a>1.6.1. &#x4E0D;&#x53EF;&#x53D8;&#x7684;&#x597D;&#x5904;</h3>
<ul>
<li>&#x53EF;&#x4EE5;&#x7F13;&#x5B58;hash&#x503C;</li>
<li>String Pool&#x7684;&#x9700;&#x8981;</li>
<li>&#x5B89;&#x5168;&#x6027;</li>
<li>&#x7EBF;&#x7A0B;&#x5B89;&#x5168;</li>
</ul>
<h3 id="string-stringbuffer-stringbuilder"><a name="string-stringbuffer-stringbuilder" class="anchor-navigation-ex-anchor" href="#string-stringbuffer-stringbuilder"><i class="fa fa-link" aria-hidden="true"></i></a>1.6.2. String StringBuffer StringBuilder</h3>
<ol>
<li>&#x53EF;&#x53D8;&#x6027;&#xFF1A;String&#x4E0D;&#x53EF;&#x53D8;&#xFF0C;&#x5176;&#x4ED6;&#x4E24;&#x4E2A;&#x53EF;&#x53D8;</li>
<li>&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#xFF1A;String&#xFF08;&#x4E0D;&#x53EF;&#x53D8;&#xFF09;StringBuffer&#xFF08;synchronized&#xFF09;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#xFF0C;StringBuilder&#x7EBF;&#x7A0B;&#x4E0D;&#x5B89;&#x5168;</li>
</ol>
<h2 id="&#x6807;&#x8BC6;&#x7B26;&#x548C;&#x5173;&#x952E;&#x5B57;"><a name="&#x6807;&#x8BC6;&#x7B26;&#x548C;&#x5173;&#x952E;&#x5B57;" class="anchor-navigation-ex-anchor" href="#&#x6807;&#x8BC6;&#x7B26;&#x548C;&#x5173;&#x952E;&#x5B57;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7. &#x6807;&#x8BC6;&#x7B26;&#x548C;&#x5173;&#x952E;&#x5B57;</h2>
<p>&#x5173;&#x952E;&#x5B57;&#x662F;&#x5177;&#x6709;&#x7279;&#x6B8A;&#x542B;&#x4E49;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#xFF0C;&#x667A;&#x80FD;&#x7528;&#x4E8E;&#x7279;&#x5B9A;&#x7684;&#x5730;&#x65B9;</p>
<blockquote>
<p>&#x5E38;&#x89C1;&#x5173;&#x952E;&#x5B57;</p>
</blockquote>
<p>&#x8BBF;&#x95EE;&#x63A7;&#x5236;&#xFF1A;private protected public</p>
<p>&#x7C7B;&#x3001;&#x65B9;&#x6CD5;&#x548C;&#x53D8;&#x91CF;&#x4FEE;&#x9970;&#x7B26;&#xFF1A;abstract class extends final implements interface native new static stricfp synchronized transient
volatile</p>
<p>&#x7A0B;&#x5E8F;&#x63A7;&#x5236;&#xFF1A;break continue return do while if else for instanceof switch case default</p>
<p>&#x9519;&#x8BEF;&#x5904;&#x7406;&#xFF1A;try catch throw throws finally</p>
<p>&#x5305;&#x76F8;&#x5173;&#xFF1A;import package</p>
<p>&#x57FA;&#x672C;&#x7C7B;&#x578B;&#xFF1A;boolean byte char double float int long short null true false</p>
<p>&#x53D8;&#x91CF;&#x5F15;&#x7528;&#xFF1A;super this void</p>
<p>&#x4FDD;&#x7559;&#x5B57;&#xFF1A;goto const</p>
<h2 id="&#x81EA;&#x589E;&#x81EA;&#x51CF;&#x8FD0;&#x7B97;&#x7B26;"><a name="&#x81EA;&#x589E;&#x81EA;&#x51CF;&#x8FD0;&#x7B97;&#x7B26;" class="anchor-navigation-ex-anchor" href="#&#x81EA;&#x589E;&#x81EA;&#x51CF;&#x8FD0;&#x7B97;&#x7B26;"><i class="fa fa-link" aria-hidden="true"></i></a>1.8. &#x81EA;&#x589E;&#x81EA;&#x51CF;&#x8FD0;&#x7B97;&#x7B26;</h2>
<p>++/--&#x653E;&#x5728;&#x53D8;&#x91CF;&#x4E4B;&#x524D;&#xFF1A;&#x5148;&#x81EA;&#x589E;/&#x81EA;&#x51CF;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x8D4B;&#x503C;</p>
<p>++/--&#x653E;&#x5728;&#x53D8;&#x91CF;&#x4E4B;&#x540E;&#xFF1A;&#x5148;&#x8D4B;&#x503C;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x81EA;&#x589E;/&#x81EA;&#x51CF;</p>
<p>&#x5373;&#x8FD0;&#x7B97;&#x7B26;&#x6240;&#x5728;&#x4F4D;&#x7F6E;&#x51B3;&#x5B9A;&#x4E86;&#x81EA;&#x589E;&#x81EA;&#x51CF;&#x6267;&#x884C;&#x7684;&#x987A;&#x5E8F;</p>
<h2 id="continue-break-return"><a name="continue-break-return" class="anchor-navigation-ex-anchor" href="#continue-break-return"><i class="fa fa-link" aria-hidden="true"></i></a>1.9. continue break return</h2>
<p>&#x5728;&#x6307;&#x5B9A;&#x6761;&#x4EF6;&#x7684;&#x5FAA;&#x73AF;&#x4E2D;&#xFF0C;&#x9700;&#x8981;&#x63D0;&#x524D;&#x7EC8;&#x6B62;&#x5FAA;&#x73AF;&#xFF1A;</p>
<p>1.continue&#xFF1A;&#x8DF3;&#x51FA;&#x5F53;&#x524D;&#x7684;&#x8FD9;&#x4E00;&#x6B21;&#x5FAA;&#x73AF;&#xFF0C;&#x7EE7;&#x7EED;&#x4E0B;&#x4E00;&#x6B21;&#x5FAA;&#x73AF;</p>
<p>2.break&#xFF1A;&#x8DF3;&#x51FA;&#x6574;&#x4E2A;&#x5FAA;&#x73AF;&#x4F53;&#xFF0C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x540E;&#x9762;&#x7684;&#x8BED;&#x53E5;</p>
<p>return&#x7528;&#x4E8E;&#x8DF3;&#x51FA;&#x6240;&#x5728;&#x65B9;&#x6CD5;&#xFF0C;&#x7ED3;&#x675F;&#x8BE5;&#x65B9;&#x6CD5;&#x7684;&#x8FD0;&#x884C;</p>
<p>1.return&#xFF1A;&#x76F4;&#x63A5;&#x4F7F;&#x7528;return&#x7ED3;&#x675F;&#x65B9;&#x6CD5;&#x6267;&#x884C;&#xFF0C;&#x7528;&#x4E8E;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x51FD;&#x6570;&#x7684;&#x65B9;&#x6CD5;</p>
<p>2.return value&#xFF1A;return&#x4E00;&#x4E2A;&#x7279;&#x5B9A;&#x503C;&#xFF0C;&#x7528;&#x4E8E;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x51FD;&#x6570;&#x7684;&#x65B9;&#x6CD5;</p>
<h2 id="java&#x6CDB;&#x578B;-&#x7C7B;&#x578B;&#x64E6;&#x9664;-&#x901A;&#x914D;&#x7B26;"><a name="java&#x6CDB;&#x578B;-&#x7C7B;&#x578B;&#x64E6;&#x9664;-&#x901A;&#x914D;&#x7B26;" class="anchor-navigation-ex-anchor" href="#java&#x6CDB;&#x578B;-&#x7C7B;&#x578B;&#x64E6;&#x9664;-&#x901A;&#x914D;&#x7B26;"><i class="fa fa-link" aria-hidden="true"></i></a>1.10. Java&#x6CDB;&#x578B; &#x7C7B;&#x578B;&#x64E6;&#x9664; &#x901A;&#x914D;&#x7B26;</h2>
<h3 id="java&#x6CDB;&#x578B;"><a name="java&#x6CDB;&#x578B;" class="anchor-navigation-ex-anchor" href="#java&#x6CDB;&#x578B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.10.1. Java&#x6CDB;&#x578B;</h3>
<p>&#x6CDB;&#x578B;generics&#xFF0C;&#x80FD;&#x5E7F;&#x6CDB;&#x4F7F;&#x7528;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4E5F;&#x53EB;&#x53C2;&#x6570;&#x5316;&#x7C7B;&#x578B;&#xFF0C;&#x6216;&#x8005;&#x8BF4;&#x5C06;&#x7C7B;&#x578B;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x4E00;&#x4E2A;&#x7C7B;&#x6216;&#x8005;&#x65B9;&#x6CD5;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">// 1.&#x57FA;&#x672C;&#x7528;&#x6CD5; -- &#x4F7F;&#x7528;&#x5F3A;&#x5236;&#x8F6C;&#x6362;</span>
<span class="token keyword">class</span> <span class="token class-name">Cache</span> <span class="token punctuation">{</span>
    <span class="token class-name">Object</span> value<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">return</span> value<span class="token punctuation">;</span><span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setValue</span><span class="token punctuation">(</span><span class="token class-name">Object</span> value<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value<span class="token punctuation">;</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Cache</span> cache <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Cache</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        cache<span class="token punctuation">.</span><span class="token function">setValue</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> value <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span> cache<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        cache<span class="token punctuation">.</span><span class="token function">setValue</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> value1 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">)</span> cache<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 2.&#x4F7F;&#x7528;&#x6CDB;&#x578B; -- &#x7701;&#x53BB;&#x5F3A;&#x5236;&#x8F6C;&#x6362;</span>
<span class="token keyword">class</span> <span class="token class-name">Cache</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    <span class="token class-name">T</span> value<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">T</span> <span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">return</span> value<span class="token punctuation">;</span><span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setValue</span><span class="token punctuation">(</span><span class="token class-name">T</span> value<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value<span class="token punctuation">;</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Cache</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> cache <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Cache</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        cache<span class="token punctuation">.</span><span class="token function">setValue</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// cache.setValue(&quot;hello&quot;); &#x62A5;&#x9519;</span>
        <span class="token keyword">int</span> value <span class="token operator">=</span> cache<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">Cache</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> cache2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Cache</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        cache2<span class="token punctuation">.</span><span class="token function">setValue</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// cache2.setValue(123); &#x62A5;&#x9519;</span>
        <span class="token class-name">String</span> value1 <span class="token operator">=</span> cache2<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>1.&#x6CDB;&#x578B;&#x4F7F;&#x5F97;&#x6570;&#x636E;&#x7684;&#x7C7B;&#x522B;&#x53EF;&#x4EE5;&#x60F3;&#x53C2;&#x6570;&#x4E00;&#x6837;&#x7531;&#x5916;&#x90E8;&#x4F20;&#x9012;&#x8FDB;&#x884C;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x6269;&#x5C55;&#x80FD;&#x529B;&#xFF0C;&#x66F4;&#x52A0;&#x7B26;&#x5408;&#x9762;&#x5411;&#x62BD;&#x8C61;&#x5F00;&#x53D1;&#x7684;&#x8F6F;&#x4EF6;&#x5B97;&#x65E8;</p>
<p>2.&#x5728;&#x6307;&#x5B9A;&#x5177;&#x4F53;&#x7C7B;&#x578B;&#x540E;&#xFF0C;&#x6CDB;&#x578B;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x7C7B;&#x578B;&#x76D1;&#x6D4B;&#x7684;&#x673A;&#x5236;&#xFF0C;&#x53EA;&#x6709;&#x76F8;&#x5339;&#x914D;&#x7684;&#x6570;&#x636E;&#x624D;&#x80FD;&#x6B63;&#x5E38;&#x7684;&#x8D4B;&#x503C;&#xFF0C;&#x5426;&#x5219;&#x7F16;&#x8BD1;&#x5668;&#x5C31;&#x4E0D;&#x901A;&#x8FC7;&#x3002;&#x6240;&#x4EE5;&#x5B83;&#x662F;&#x4E00;&#x79CD;&#x7C7B;&#x578B;&#x5B89;&#x5168;&#x68C0;&#x6D4B;&#x673A;&#x5236;</p>
<p>3.&#x6CDB;&#x578B;&#x63D0;&#x9AD8;&#x4E86;&#x7A0B;&#x5E8F;&#x4EE3;&#x7801;&#x7684;&#x53EF;&#x8BFB;&#x6027;&#xFF0C;&#x4E0D;&#x5FC5;&#x8981;&#x7B49;&#x5230;&#x8FD0;&#x884C;&#x7684;&#x65F6;&#x5019;&#x624D;&#x53BB;&#x5F3A;&#x5236;&#x8F6C;&#x6362;&#xFF0C;&#x5728;&#x5B9A;&#x4E49;&#x6216;&#x8005;&#x5B9E;&#x4F8B;&#x5316;&#x9636;&#x6BB5;&#xFF0C;Cache<string>&#x53EF;&#x4EE5;&#x663E;&#x5316;&#x7684;&#x5224;&#x65AD;&#x51FA;&#x64CD;&#x4F5C;&#x7684;&#x7C7B;&#x578B;&#x6570;&#x636E;</string></p>
<blockquote>
<p>&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x89C4;&#x8303;</p>
</blockquote>
<ul>
<li>T &#x4EE3;&#x8868;&#x4E00;&#x822C;&#x7684;&#x4EFB;&#x4F55;&#x7C7B;</li>
<li>E &#x4EE3;&#x8868;Element&#x6216;Exception</li>
<li>K &#x4EE3;&#x8868;Key</li>
<li>V &#x4EE3;&#x8868;Value&#xFF0C;&#x901A;&#x5E38;&#x4E0E;K&#x914D;&#x5408;&#x4F7F;&#x7528;</li>
<li>S &#x4EE3;&#x8868;Subtype</li>
</ul>
<pre class="language-"><code class="lang-java"><span class="token comment">// 1.&#x6CDB;&#x578B;&#x7C7B;</span>
<span class="token comment">// &#x5355;&#x4E2A;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x7684;&#x6CDB;&#x578B;&#x7C7B;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    <span class="token class-name">T</span> field<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
    <span class="token comment">// &#x6CDB;&#x578B;&#x7C7B;&#x7684;&#x4F7F;&#x7528;</span>
    <span class="token class-name">Test</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// &#x591A;&#x4E2A;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x7684;&#x6CDB;&#x578B;&#x7C7B;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">E</span><span class="token punctuation">,</span> <span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    <span class="token class-name">E</span> filed1<span class="token punctuation">;</span>
    <span class="token class-name">T</span> field2<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 2.&#x6CDB;&#x578B;&#x65B9;&#x6CD5;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token comment">/*
    &#x6CDB;&#x578B;&#x65B9;&#x6CD5;&#x4E0E;&#x6CDB;&#x578B;&#x7C7B;&#x4E0D;&#x540C;&#x7684;&#x5730;&#x65B9;&#x662F;&lt;T&gt;&#x5728;&#x8FD4;&#x56DE;&#x503C;&#x524D;&#x9762;
    &lt;T&gt;&#x4E2D;&#x7684;T&#x88AB;&#x79F0;&#x4E3A;&#x7C7B;&#x578B;&#x53C2;&#x6570;
    (T t)&#x4E2D;&#x7684;T&#x88AB;&#x79F0;&#x4E3A;&#x53C2;&#x6570;&#x5316;&#x7C7B;&#x578B;&#xFF0C;&#x5E76;&#x975E;&#x8FD0;&#x884C;&#x65F6;&#x771F;&#x6B63;&#x7684;&#x53C2;&#x6570;
    */</span>
    <span class="token keyword">public</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">void</span> <span class="token function">testMethod</span><span class="token punctuation">(</span><span class="token class-name">T</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 3.&#x6CDB;&#x578B;&#x63A5;&#x53E3;</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Iterable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6CDB;&#x578B;&#x7C7B;&#x4E0E;&#x6CDB;&#x578B;&#x65B9;&#x6CD5;&#x5171;&#x7528;&#x65F6;&#xFF0C;&#x6CDB;&#x578B;&#x65B9;&#x6CD5;&#x4E0E;&#x6CDB;&#x578B;&#x7C7B;&#x7684;&#x7C7B;&#x578B;&#x662F;&#x5404;&#x81EA;&#x6307;&#x5B9A;&#x7684;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">// 1.&#x6CDB;&#x578B;&#x7C7B;&#x4E0E;&#x6CDB;&#x578B;&#x65B9;&#x6CD5;&#x5171;&#x5B58;</span>
<span class="token comment">// &#x6CDB;&#x578B;&#x7C7B;</span>
<span class="token keyword">class</span> <span class="token class-name">Generic</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x666E;&#x901A;&#x65B9;&#x6CD5;</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testMethod</span><span class="token punctuation">(</span><span class="token class-name">T</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>t<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x6CDB;&#x578B;&#x65B9;&#x6CD5;</span>
    <span class="token keyword">public</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token class-name">T</span> <span class="token function">testGenericMethod</span><span class="token punctuation">(</span><span class="token class-name">T</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> t<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x4EE5;&#x4E0A;&#x6CDB;&#x578B;&#x7C7B;&#x4E0E;&#x6CDB;&#x578B;&#x65B9;&#x6CD5;&#x7684;T&#x4E00;&#x6837;&#xFF0C;&#x4F46;&#x662F;&#x4EE5;&#x4E0B;&#x7C7B;&#x578B;&#x4E0D;&#x540C;</span>
        <span class="token comment">// &#x53EF;&#x4EE5;&#x770B;&#x51FA;&#x6CDB;&#x578B;&#x65B9;&#x6CD5;&#x7684;&#x7C7B;&#x578B;&#x662F;&#x6839;&#x636E;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x4F20;&#x9012;&#x7684;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x51B3;&#x5B9A;</span>
        <span class="token class-name">Generic</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> t <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Generic</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        t<span class="token punctuation">.</span><span class="token function">testMethod</span><span class="token punctuation">(</span><span class="token string">&quot;show&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Integer</span> i <span class="token operator">=</span> t<span class="token punctuation">.</span><span class="token function">testGenericMethod</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 2.&#x4E3A;&#x533A;&#x522B;&#x4EE5;&#x4E0A;&#x7279;&#x70B9;&#xFF0C;&#x5C06;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x547D;&#x540D;&#x8FDB;&#x884C;&#x4FEE;&#x6539;</span>
<span class="token keyword">class</span> <span class="token class-name">Generic</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testMethod</span><span class="token punctuation">(</span><span class="token class-name">T</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>t<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">E</span><span class="token punctuation">&gt;</span></span> <span class="token class-name">E</span> <span class="token function">testGenericMethod</span><span class="token punctuation">(</span><span class="token class-name">E</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> e<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x901A;&#x914D;&#x7B26;"><a name="&#x901A;&#x914D;&#x7B26;" class="anchor-navigation-ex-anchor" href="#&#x901A;&#x914D;&#x7B26;"><i class="fa fa-link" aria-hidden="true"></i></a>1.10.2. &#x901A;&#x914D;&#x7B26;</h3>
<ul>
<li>&lt;?&gt;                      &#x88AB;&#x79F0;&#x4F5C;&#x65E0;&#x9650;&#x5B9A;&#x7684;&#x901A;&#x914D;&#x7B26;</li>
<li>&lt;? extends T&gt;    &#x88AB;&#x79F0;&#x4F5C;&#x6709;&#x4E0A;&#x9650;&#x7684;&#x901A;&#x914D;&#x7B26;</li>
<li>&lt;? super T&gt;        &#x88AB;&#x79F0;&#x4F5C;&#x6709;&#x4E0B;&#x9650;&#x7684;&#x901A;&#x914D;&#x7B26;</li>
</ul>
<p>&#x901A;&#x914D;&#x7B26;&#x89E3;&#x51B3;&#x7684;&#x95EE;&#x9898;&#xFF1A;&#x5177;&#x6709;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#x7684;&#x7C7B;&#xFF0C;&#x5176;&#x5BF9;&#x5E94;&#x7684;&#x6CDB;&#x578B;&#x4E0D;&#x5177;&#x5907;&#x7EE7;&#x627F;&#x5173;&#x7CFB;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">class</span> <span class="token class-name">Base</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">Sub</span> <span class="token keyword">extends</span> <span class="token class-name">Base</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
    <span class="token comment">// Base&#x548C;Sub&#x662F;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#xFF0C;Sub&#x7684;&#x5B9E;&#x4F8B;&#x53EF;&#x4EE5;&#x7ED9;Base&#x5F15;&#x7528;&#x8D4B;&#x503C;</span>
    <span class="token class-name">Sub</span> sub <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Sub</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Base</span> base <span class="token operator">=</span> sub<span class="token punctuation">;</span>
    <span class="token comment">// List&lt;Sub&gt;&#x548C;List&lt;Base&gt;&#x4E0D;&#x5177;&#x6709;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#xFF0C;&#x56E0;&#x6B64;&#x4EE5;&#x4E0B;&#x64CD;&#x4F5C;&#x62A5;&#x9519;</span>
<span class="token comment">// List&lt;Sub&gt;&#x7684;&#x5B9E;&#x4F8B;&#x4E0D;&#x80FD;&#x7ED9;List&lt;Base&gt;&#x7684;&#x5F15;&#x7528;&#x8D4B;&#x503C;</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Sub</span><span class="token punctuation">&gt;</span></span> lsub <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Base</span><span class="token punctuation">&gt;</span></span> lbase <span class="token operator">=</span> lsub<span class="token punctuation">;</span> <span class="token comment">//&#x62A5;&#x9519;</span>
    <span class="token comment">// List&lt;Base&gt;&#x7684;&#x5B9E;&#x4F8B;&#x4E0D;&#x80FD;&#x7ED9;List&lt;Sub&gt;&#x7684;&#x5F15;&#x7528;&#x8D4B;&#x503C;</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Base</span><span class="token punctuation">&gt;</span></span> lbase <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Sub</span><span class="token punctuation">&gt;</span></span> lsub <span class="token operator">=</span> lbase<span class="token punctuation">;</span> <span class="token comment">//&#x62A5;&#x9519;</span>
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x539F;&#x56E0;&#x5206;&#x6790;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">class</span> <span class="token class-name">A</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">B</span> <span class="token keyword">extends</span> <span class="token class-name">A</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">C</span> <span class="token keyword">extends</span> <span class="token class-name">A</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
    <span class="token comment">/*
    1.&#x5047;&#x8BBE;listA = listB&#x5408;&#x6CD5;
    &#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;listA&#x5F15;&#x7528;&#x63D2;&#x5165;A&#x548C;C&#x7684;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;
    &#x56E0;&#x6B64;&#x63D2;&#x5165;&#x975E;B&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x5230;&#x4E00;&#x4E2A;&#x6301;&#x6709;B&#xFF08;B&#x5B50;&#x7C7B;&#xFF09;&#x5B9E;&#x4F8B;&#x7684;list&#x4E2D;
    */</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">B</span><span class="token punctuation">&gt;</span></span> listB <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">A</span><span class="token punctuation">&gt;</span></span> listA <span class="token operator">=</span> listB<span class="token punctuation">;</span>
    <span class="token comment">/*
    2.&#x5047;&#x8BBE;listB = listA&#x5408;&#x6CD5;
    &#x7531;&#x4E8E;List&lt;A&gt;&#x53EF;&#x4EE5;&#x63D2;&#x5165;A&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x6BD4;&#x5982;B&#x548C;C
    &#x56E0;&#x6B64;listB&#x53D6;&#x51FA;&#x7684;&#x503C;&#x4F1A;&#x51FA;&#x73B0;&#x975E;B&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x8FDD;&#x80CC;&#x4E86;List&lt;B&gt;&#x5B9A;&#x4E49;&#x65F6;&#x7684;&#x7EA6;&#x5B9A;
    */</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">A</span><span class="token punctuation">&gt;</span></span> listA <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">B</span><span class="token punctuation">&gt;</span></span> listB <span class="token operator">=</span> listA<span class="token punctuation">;</span>
<span class="token comment">/*
&#x4EE5;&#x4E0A;&#x4E24;&#x8005;&#x90FD;&#x662F;&#x540C;&#x4E00;&#x79CD;&#x95EE;&#x9898;&#xFF1A;
&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7236;&#x7C7B;A&#x6CDB;&#x578B;&#x5F15;&#x7528;&#x63D2;&#x5165;&#x975E;&#x5F53;&#x524D;&#x7C7B;B&#x7684;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x5230;&#x96C6;&#x5408;&#x4E2D;
&#x8FD9;&#x5FC5;&#x7136;&#x5BFC;&#x81F4;&#x7834;&#x574F;&#x4E86;B&#x6CDB;&#x578B;&#x5B9E;&#x4F8B;&#x7684;&#x5B9A;&#x4E49;
*/</span>
</code></pre>
<p>&#x65E0;&#x754C;&#x5B9A;&#x901A;&#x914D;&#x7B26;&#xFF1F;&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x6301;&#x6709;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x56E0;&#x4E3A;&#x7C7B;&#x578B;&#x4E0D;&#x786E;&#x5B9A;&#xFF0C;&#x6240;&#x4EE5;&#x53EA;&#x80FD;&#x8BFB;</p>
<p>&#x4E0A;&#x754C;&#x901A;&#x914D;&#x7B26;(? extends T) &#x4EE3;&#x8868;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x6301;&#x6709;T&#x53CA;&#x5176;&#x5B50;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x6B64;&#x65F6;&#x4ECE;&#x96C6;&#x5408;&#x4E2D;&#x8BFB;&#x51FA;&#x5143;&#x7D20;&#x5E76;&#x5F3A;&#x8F6C;&#x4E3A;T&#x7C7B;&#x578B;&#x662F;&#x5B89;&#x5168;&#x7684;</p>
<p>&#x4E0B;&#x754C;&#x901A;&#x914D;&#x7B26;(? super T) &#x4EE3;&#x8868;&#x7684;&#x662F;&#x6301;&#x6709;T&#x53CA;&#x5176;&#x7236;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x6B64;&#x65F6;&#x53EF;&#x4EE5;&#x63D2;&#x5165;T&#x53CA;&#x5176;&#x5B50;&#x7C7B;&#x5230;&#x96C6;&#x5408;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x53D6;&#x5143;&#x7D20;&#x9700;&#x8981;&#x5F3A;&#x8F6C;&#x4E3A;Object&#x7C7B;&#x578B;</p>
<h3 id="&#x7C7B;&#x578B;&#x64E6;&#x9664;"><a name="&#x7C7B;&#x578B;&#x64E6;&#x9664;" class="anchor-navigation-ex-anchor" href="#&#x7C7B;&#x578B;&#x64E6;&#x9664;"><i class="fa fa-link" aria-hidden="true"></i></a>1.10.3. &#x7C7B;&#x578B;&#x64E6;&#x9664;</h3>
<p>&#x6CDB;&#x578B;&#x662F;Java1.5&#x5F15;&#x5165;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x6CDB;&#x578B;&#x4FE1;&#x606F;&#x53EA;&#x5B58;&#x5728;&#x4E8E;&#x4EE3;&#x7801;&#x7F16;&#x8BD1;&#x9636;&#x6BB5;&#xFF0C;&#x5728;&#x8FDB;&#x5165;JVM&#x4E4B;&#x524D;&#xFF0C;&#x4E0E;&#x6CDB;&#x578B;&#x76F8;&#x5173;&#x7684;&#x4FE1;&#x606F;&#x4F1A;&#x88AB;&#x64E6;&#x9664;&#x6389;&#xFF0C;&#x4E13;&#x4E1A;&#x672F;&#x8BED;&#x53EB;&#x505A;&#x7C7B;&#x578B;&#x64E6;&#x9664;</p>
<pre class="language-"><code class="lang-java"><span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> list<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// &#x76F4;&#x63A5;&#x6DFB;&#x52A0;&#x62A5;&#x9519;</span>
<span class="token comment">// list.add(&quot;a&quot;);</span>
        <span class="token class-name">Class</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token operator">?</span><span class="token keyword">extends</span> <span class="token class-name">List</span><span class="token punctuation">&gt;</span></span> clazz<span class="token operator">=</span>list<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Method</span> add<span class="token operator">=</span>clazz<span class="token punctuation">.</span><span class="token function">getDeclaredMethod</span><span class="token punctuation">(</span><span class="token string">&quot;add&quot;</span><span class="token punctuation">,</span><span class="token class-name">Object</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// &#x901A;&#x8FC7;&#x53CD;&#x5C04;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;</span>
        add<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span><span class="token string">&quot;k1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> l1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> l2<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>l1<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">==</span>l2<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h2 id="this"><a name="this" class="anchor-navigation-ex-anchor" href="#this"><i class="fa fa-link" aria-hidden="true"></i></a>1.11. this</h2>
<ul>
<li>this&#x7528;&#x4E8E;&#x5F15;&#x7528;&#x5F53;&#x524D;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;</li>
<li>this&#x7528;&#x4E8E;&#x8C03;&#x7528;&#x5F53;&#x524D;&#x7C7B;&#x65B9;&#x6CD5;&#xFF08;&#x9690;&#x5F0F;&#xFF09;</li>
<li>this()&#x7528;&#x6765;&#x8C03;&#x7528;&#x5F53;&#x524D;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;</li>
<li>this&#x4F5C;&#x4E3A;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x4E2D;&#x7684;&#x53C2;&#x6570;&#x4F20;&#x9012;</li>
<li>this&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x4F20;&#x9012;</li>
<li>this&#x7528;&#x4E8E;&#x4ECE;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;</li>
</ul>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> num<span class="token punctuation">;</span>
    <span class="token class-name">String</span> str<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token keyword">int</span> num<span class="token punctuation">,</span> <span class="token class-name">String</span> str<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x7531;&#x4E8E;&#x5F62;&#x53C2;num/str&#x4E0E;&#x5168;&#x5C40;&#x53D8;&#x91CF;num/str&#x76F8;&#x540C;</span>
        <span class="token comment">// &#x4EE5;&#x4E0B;&#x64CD;&#x4F5C;&#x53EA;&#x5BF9;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x6709;&#x6548;&#xFF0C;&#x5BF9;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x65E0;&#x6548;</span>
        num <span class="token operator">=</span> num<span class="token punctuation">;</span>
        str <span class="token operator">=</span> str<span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>num <span class="token operator">+</span> <span class="token string">&quot; &quot;</span> <span class="token operator">+</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>num <span class="token operator">+</span> <span class="token string">&quot; &quot;</span> <span class="token operator">+</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span> test1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token string">&quot;show&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Test</span> test2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token string">&quot;hide&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        test1<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        test2<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 1. this&#x5F15;&#x7528;&#x5F53;&#x524D;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#xFF0C;&#x89E3;&#x51B3;&#x4EE5;&#x4E0A;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x95EE;&#x9898;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> num<span class="token punctuation">;</span>
    <span class="token class-name">String</span> str<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token keyword">int</span> num<span class="token punctuation">,</span> <span class="token class-name">String</span> str<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>num <span class="token operator">=</span> num<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>str <span class="token operator">=</span> str<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>num <span class="token operator">+</span> <span class="token string">&quot; &quot;</span> <span class="token operator">+</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span> test1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token string">&quot;show&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Test</span> test2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token string">&quot;hide&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        test1<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        test2<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 2. &#x8C03;&#x7528;&#x5F53;&#x524D;&#x7C7B;&#x65B9;&#x6CD5;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x4EE5;&#x4E0B;&#x4E24;&#x8005;&#x7B49;&#x6548;</span>
        <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x4E0D;&#x4F7F;&#x7528;this&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x5728;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x65F6;&#x81EA;&#x52A8;&#x6DFB;&#x52A0;this&#x5173;&#x952E;&#x5B57;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 3. this() &#x8C03;&#x7528;&#x5F53;&#x524D;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;</span>
<span class="token comment">//    this()&#x5FC5;&#x987B;&#x662F;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x8BED;&#x53E5;&#xFF0C;&#x5426;&#x5219;&#x4F1A;&#x62A5;&#x9519;</span>
<span class="token comment">//    this()&#x7EF4;&#x62A4;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E4B;&#x95F4;&#x7684;&#x94FE;&#xFF0C;&#x5373;&#x5B83;&#x7528;&#x4E8E;&#x6784;&#x9020;&#x51FD;&#x6570;&#x94FE;&#x63A5;&#xFF1A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x76F8;&#x4E92;&#x8C03;&#x7528;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;constructor&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x6709;&#x6B8B;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x8C03;&#x7528;&#x65E0;&#x53C2;&#x6784;&#x9020;&#x51FD;&#x6570;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x65E0;&#x53C2;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x8C03;&#x7528;&#x6709;&#x53C2;&#x6784;&#x9020;&#x51FD;&#x6570;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;constructor&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 4.&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x591A;&#x4E2A;&#x65B9;&#x6CD5;&#x6216;&#x591A;&#x4E2A;&#x7C7B;&#x4E2D;&#x91CD;&#x7528;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;</span>
<span class="token keyword">class</span> <span class="token class-name">Demo</span> <span class="token punctuation">{</span>
    <span class="token class-name">Test</span> test<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Demo</span><span class="token punctuation">(</span><span class="token class-name">Test</span> test<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>test <span class="token operator">=</span> test<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Demo:&quot;</span> <span class="token operator">+</span> test<span class="token punctuation">.</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> data <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Demo</span> demo <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Demo</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        demo<span class="token punctuation">.</span><span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 5.&#x7528;&#x6765;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x7C7B;&#x7684;&#x5BF9;&#x8C61;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Test</span> <span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="lambda"><a name="lambda" class="anchor-navigation-ex-anchor" href="#lambda"><i class="fa fa-link" aria-hidden="true"></i></a>1.12. Lambda</h2>
<blockquote>
<p>Lambda&#x8BED;&#x6CD5;&#x53CA;&#x5206;&#x7C7B;</p>
</blockquote>
<p>Lambda&#x8868;&#x8FBE;&#x5F0F;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x4E00;&#x4E2A;&#x533F;&#x540D;&#x51FD;&#x6570;&#xFF1B;Lambda&#x8868;&#x8FBE;&#x5F0F;&#x5141;&#x8BB8;&#x5C06;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;</p>
<p>Lambda&#x8868;&#x8FBE;&#x5F0F;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x662F;&#x4E00;&#x6BB5;&#x53EF;&#x4EE5;&#x4F20;&#x9012;&#x7684;&#x4EE3;&#x7801;&#xFF08;&#x5C06;&#x4EE3;&#x7801;&#x4F5C;&#x4E3A;&#x5B9E;&#x53C2;&#xFF09;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x51FD;&#x6570;&#x5F0F;&#x7F16;&#x7A0B;&#xFF0C;&#x5C06;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x4F20;&#x9012;</p>
<p>Lambda&#x8868;&#x8FBE;&#x5F0F;&#x80FD;&#x591F;&#x8BA9;&#x7F16;&#x7A0B;&#x66F4;&#x52A0;&#x7B80;&#x6D01;&#x9AD8;&#x6548;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">class</span> <span class="token class-name">MyRunnable</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Thread</span> thread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MyRunnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread<span class="token punctuation">.</span><span class="token function">interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4F7F;&#x7528;&#x533F;&#x540D;&#x5185;&#x90E8;&#x7C7B;&#x8FDB;&#x884C;&#x91CD;&#x6784;&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">// 1.&#x4F7F;&#x7528;&#x5F15;&#x7528;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Thread</span> thread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 2.&#x4E0D;&#x4F7F;&#x7528;&#x5F15;&#x7528;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4F7F;&#x7528;Lambda&#x91CD;&#x6784;&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>Lambda&#x8BED;&#x6CD5;&#xFF1A;<code>([ Labmda&#x53C2;&#x6570;&#x5217;&#x8868;&#xFF0C;&#x5373;&#x5F62;&#x53C2;&#x5217;&#x8868; ]) -&gt; { Lambda&#x4F53;&#xFF0C;&#x5373;&#x65B9;&#x6CD5;&#x4F53; }</code></p>
<p>&#x5206;&#x7C7B;&#xFF1A;&#x65E0;&#x53C2;&#x65E0;&#x8FD4;&#x56DE;&#x3001;&#x6709;&#x53C2;&#x65E0;&#x8FD4;&#x56DE;&#x3001;&#x65E0;&#x53C2;&#x6709;&#x8FD4;&#x56DE;&#x3001;&#x6709;&#x53C2;&#x6709;&#x8FD4;&#x56DE;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">// &#x65E0;&#x53C2;&#x65E0;&#x8FD4;&#x56DE;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x6709;&#x53C2;&#x65E0;&#x8FD4;&#x56DE;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;b&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;c&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>t <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// &#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x5F15;&#x7528;</span>
        <span class="token comment">// list.forEach(System.out::println);</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x65E0;&#x53C2;&#x6709;&#x8FD4;&#x56DE;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Random</span> random <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;100&#x4EE5;&#x5185;&#x7684;&#x6B63;&#x6574;&#x6570;</span>
        <span class="token class-name">Stream</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> stream <span class="token operator">=</span> <span class="token class-name">Stream</span><span class="token punctuation">.</span><span class="token function">generate</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> random<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// &#x4E0D;&#x65AD;&#x6253;&#x5370;100&#x4EE5;&#x5185;&#x7684;&#x6B63;&#x6574;&#x6570;</span>
        stream<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x6709;&#x53C2;&#x6709;&#x8FD4;&#x56DE;</span>
<span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">,</span> <span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">return</span> age<span class="token punctuation">;</span><span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span><span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">return</span> name<span class="token punctuation">;</span><span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span><span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;Student{&quot;</span> <span class="token operator">+</span> <span class="token string">&quot;age=&quot;</span> <span class="token operator">+</span> age <span class="token operator">+</span> <span class="token string">&quot;, name=&apos;&quot;</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token char">&apos;\&apos;&apos;</span> <span class="token operator">+</span> <span class="token char">&apos;}&apos;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Collator</span> collator <span class="token operator">=</span> <span class="token class-name">Collator</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Student</span><span class="token punctuation">&gt;</span></span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> s2<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> collator<span class="token punctuation">.</span><span class="token function">compare</span><span class="token punctuation">(</span>s1<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token string">&quot;&#x5F20;&#x4E09;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">&quot;&#x56DB;&#x5B9D;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;&#x963F;&#x5F3A;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>student <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>student<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;</p>
</blockquote>
<p>&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;&#x5373;SAM&#xFF08;Single Abstract Method&#xFF09;&#x63A5;&#x53E3;&#xFF0C;&#x6709;&#x4E14;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;&#x7684;&#x63A5;&#x53E3;</p>
<p>&#x53EF;&#x4EE5;&#x6709;&#x9ED8;&#x8BA4;&#x65B9;&#x6CD5;&#x6216;&#x8005;&#x662F;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x548C;&#x4ECE;Object&#x7EE7;&#x627F;&#x6765;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x662F;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;&#x6709;&#x4E14;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;</p>
<p>JDK1.8&#x4E4B;&#x540E;&#x6DFB;&#x52A0;@FunctionalInterface&#x8868;&#x793A;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;</p>
<p>&#x56E0;&#x4E3A;&#x6709;&#x4E86;@FunctionalInterface&#x6807;&#x8BB0;&#xFF0C;&#x4E5F;&#x79F0;&#x8FD9;&#x6837;&#x7684;&#x63A5;&#x53E3;&#x4E3A;Mark&#xFF08;&#x6807;&#x8BB0;&#xFF09;&#x7C7B;&#x578B;&#x7684;&#x63A5;&#x53E3;</p>
<pre class="language-"><code class="lang-java">
<span class="token annotation punctuation">@FunctionalInterface</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">abstract</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x53EA;&#x6709;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;&#x7684;&#x53D8;&#x91CF;&#x6216;&#x8005;&#x662F;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;&#xFF0C;&#x624D;&#x80FD;&#x591F;&#x8D4B;&#x503C;&#x4E3A;Lambda&#x8868;&#x8FBE;&#x5F0F;</p>
<p>&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x9700;&#x8981;&#x4F20;&#x5165;&#x4E00;&#x4E2A;@FunctionalInterface&#x6807;&#x8BB0;&#x7684;&#x63A5;&#x53E3;&#x7684;&#x5B9E;&#x73B0;&#x7C7B;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;Lambda&#x8868;&#x8FBE;&#x5F0F;</p>
<p>&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;&#x80FD;&#x591F;&#x63A5;&#x53D7;&#x533F;&#x540D;&#x5185;&#x90E8;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x533F;&#x540D;&#x5185;&#x90E8;&#x7C7B;&#x6765;&#x5B9E;&#x4F8B;&#x5316;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;&#x7684;&#x5BF9;&#x8C61;</p>
<p>&#x800C;Lambda&#x8868;&#x8FBE;&#x5F0F;&#x80FD;&#x591F;&#x4EE3;&#x66FF;&#x5185;&#x90E8;&#x7C7B;&#x5B9E;&#x73B0;&#x4EE3;&#x7801;&#x7684;&#x8FDB;&#x4E00;&#x6B65;&#x7B80;&#x5316;</p>
<p>&#x56E0;&#x6B64;&#xFF0C;Lambda&#x8868;&#x8FBE;&#x5F0F;&#x548C;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;&#x7D27;&#x5BC6;&#x7684;&#x8054;&#x7CFB;&#x5230;&#x4E86;&#x4E00;&#x8D77;</p>
<p><strong>&#x6BCF;&#x4E00;&#x4E2A;Lambda&#x8868;&#x8FBE;&#x5F0F;&#x80FD;&#x9690;&#x5F0F;&#x7684;&#x7ED9;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;&#x8D4B;&#x503C;</strong></p>
<p>&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x81EA;&#x52A8;&#x5C06;Lambda&#x8868;&#x8FBE;&#x5F0F;&#x8D4B;&#x503C;&#x7ED9;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;</p>
<p>&#x53C2;&#x6570;&#x4E0E;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x4E00;&#x4E00;&#x5BF9;&#x5E94;&#x7684;&#xFF0C;&#x5373;&#x5982;&#x679C;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;&#x4E2D;&#x7684;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;&#x662F;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x6709;&#x53C2;&#x6570;&#x7684;&#xFF0C;&#x5219;Lambda&#x8868;&#x8FBE;&#x5F0F;&#x4E5F;&#x5FC5;&#x987B;&#x662F;&#x53C8;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x6709;&#x53C2;&#x6570;&#x7684;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">// &#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;</span>
<span class="token annotation punctuation">@FunctionalInterface</span>
<span class="token keyword">interface</span> <span class="token class-name">MyInterface</span> <span class="token punctuation">{</span>
    <span class="token keyword">void</span> <span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">MyInterface</span> myInterface <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        myInterface<span class="token punctuation">.</span><span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x56DB;&#x5927;&#x51FD;&#x6570;&#x5F0F;&#x63A5;&#x53E3;</p>
</blockquote>
<ol>
<li>&#x6D88;&#x8D39;&#x578B;&#x63A5;&#x53E3;&#xFF1A;Consumer <t> void accept(T t) &#x6709;&#x53C2;&#x6570;&#xFF0C;&#x65E0;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;</t></li>
<li>&#x4F9B;&#x7ED9;&#x578B;&#x63A5;&#x53E3;&#xFF1A;Supplier <t> T get() &#x65E0;&#x53C2;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;</t></li>
<li>&#x65AD;&#x5B9A;&#x578B;&#x63A5;&#x53E3;&#xFF1A;Predicate <t> boolean test(T t) &#x6709;&#x53C2;&#x6709;boolean&#x8FD4;&#x56DE;&#x503C;</t></li>
<li>&#x51FD;&#x6570;&#x578B;&#x63A5;&#x53E3;&#xFF1A;Function <t, r=""> R apply(T t) &#x6709;&#x53C2;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;</t,></li>
</ol>
<blockquote>
<p>&#x65B9;&#x6CD5;&#x5F15;&#x7528;</p>
</blockquote>
<p>&#x5F53;Lambda&#x8868;&#x8FBE;&#x5F0F;&#x6EE1;&#x8DB3;&#x67D0;&#x79CD;&#x6761;&#x4EF6;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x5F15;&#x7528;&#xFF0C;&#x53EF;&#x4EE5;&#x518D;&#x6B21;&#x7B80;&#x5316;&#x4EE3;&#x7801;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Supplier</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Student</span><span class="token punctuation">&gt;</span></span> s <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 1.&#x6784;&#x9020;&#x5F15;&#x7528;&#xFF08;Lambda&#x8868;&#x8FBE;&#x5F0F;&#x901A;&#x8FC7;new&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x6765;&#x5B8C;&#x6210;&#xFF09;</span>
        <span class="token class-name">Supplier</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Student</span><span class="token punctuation">&gt;</span></span> s <span class="token operator">=</span> <span class="token class-name">Student</span><span class="token operator">::</span><span class="token keyword">new</span><span class="token punctuation">;</span>

        <span class="token comment">// TreeSet&lt;String&gt; set = new TreeSet&lt;&gt;((s1, s2) -&gt; s1.compareTo(s2));</span>
        <span class="token comment">// 2.&#x7C7B;&#x540D;::&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</span>
        <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token operator">::</span><span class="token function">compareTo</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;Hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;isea_you&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// set.forEach(t -&gt; System.out.println(t));</span>
        <span class="token comment">// 3.&#x5BF9;&#x8C61;::&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</span>
        set<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// Stream&lt;Double&gt; stream = Stream.generate(() -&gt; Math.random());</span>
        <span class="token comment">// 4.&#x7C7B;&#x540D;::&#x9759;&#x6001;&#x65B9;&#x6CD5;</span>
        <span class="token class-name">Stream</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Double</span><span class="token punctuation">&gt;</span></span> stream <span class="token operator">=</span> <span class="token class-name">Stream</span><span class="token punctuation">.</span><span class="token function">generate</span><span class="token punctuation">(</span><span class="token class-name">Math</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        stream<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="object"><a name="object" class="anchor-navigation-ex-anchor" href="#object"><i class="fa fa-link" aria-hidden="true"></i></a>1.13. Object</h2>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Object</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">native</span> <span class="token keyword">int</span> <span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">equals</span><span class="token punctuation">(</span><span class="token class-name">Object</span> obj<span class="token punctuation">)</span>

    <span class="token keyword">protected</span> <span class="token keyword">native</span> <span class="token class-name">Object</span> <span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">CloneNotSupportedException</span>

    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">native</span> <span class="token class-name">Class</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token operator">?</span><span class="token punctuation">&gt;</span></span> <span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">finalize</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Throwable</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">native</span> <span class="token keyword">void</span> <span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">native</span> <span class="token keyword">void</span> <span class="token function">notifyAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">native</span> <span class="token keyword">void</span> <span class="token function">wait</span><span class="token punctuation">(</span><span class="token keyword">long</span> timeout<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span>

    <span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">void</span> <span class="token function">wait</span><span class="token punctuation">(</span><span class="token keyword">long</span> timeout<span class="token punctuation">,</span> <span class="token keyword">int</span> nanos<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span>

    <span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">void</span> <span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="equals"><a name="equals" class="anchor-navigation-ex-anchor" href="#equals"><i class="fa fa-link" aria-hidden="true"></i></a>1.13.1. equals()</h3>
<p>&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x5177;&#x6709;&#x7B49;&#x4EF7;&#x5173;&#x7CFB;&#xFF0C;&#x9700;&#x8981;&#x6EE1;&#x8DB3;&#x4EE5;&#x4E0B;&#x4E94;&#x4E2A;&#x6761;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">// 1. &#x81EA;&#x53CD;&#x6027;&#xFF1A;</span>
x<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token comment">// true</span>
<span class="token comment">// 2. &#x5BF9;&#x79F0;&#x6027;&#xFF1A;</span>
        x<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token operator">==</span>y<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token comment">// 3. &#x4F20;&#x9012;&#x6027;&#xFF1A;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token operator">&amp;&amp;</span>y<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>z<span class="token punctuation">)</span><span class="token punctuation">)</span>x<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>z<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token comment">// 4. &#x4E00;&#x81F4;&#x6027;&#xFF1A;&#x591A;&#x6B21;&#x8C03;&#x7528;&#x7ED3;&#x679C;&#x4E0D;&#x53D8;</span>
        x<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token operator">==</span>x<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// true</span>
<span class="token comment">// 5. &#x4E0E;null&#x6BD4;&#x8F83;&#xFF1A;</span>
        x<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
</code></pre>
<p>&#x7B49;&#x4EF7;&#x4E0E;&#x76F8;&#x7B49;</p>
<ul>
<li>&#x57FA;&#x672C;&#x7C7B;&#x578B;&#xFF1A;==&#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x503C;&#x76F8;&#x7B49;&#xFF0C;&#x6CA1;&#x6709;equals&#x65B9;&#x6CD5;</li>
<li>&#x5F15;&#x7528;&#x7C7B;&#x578B;&#xFF1A;==&#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x5F15;&#x7528;&#x662F;&#x5426;&#x76F8;&#x540C;&#xFF0C;equals&#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x7B49;&#x4EF7;</li>
</ul>
<p>equals&#x5B9E;&#x73B0;&#x601D;&#x8DEF;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">/*
1. &#x5224;&#x65AD;&#x5F15;&#x7528;&#x662F;&#x5426;&#x76F8;&#x540C;&#xFF0C;&#x662F;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;true
2. &#x5224;&#x65AD;&#x7C7B;&#x578B;&#x662F;&#x5426;&#x76F8;&#x540C;&#xFF0C;&#x4E0D;&#x662F;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;false
3. &#x5C06;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x8F6C;&#x578B;
4. &#x5224;&#x65AD;&#x6BCF;&#x4E2A;&#x5173;&#x952E;&#x57DF;&#x662F;&#x5426;&#x76F8;&#x540C;
*/</span>
<span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">String</span>
        <span class="token keyword">implements</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span>Serializable</span><span class="token punctuation">,</span> <span class="token class-name">Comparable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">,</span> <span class="token class-name">CharSequence</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">char</span> value<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">.</span>value<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">equals</span><span class="token punctuation">(</span><span class="token class-name">Object</span> anObject<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// this&#x5F53;&#x524D;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token operator">==</span> anObject<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>anObject <span class="token keyword">instanceof</span> <span class="token class-name">String</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">String</span> anotherString <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">)</span> anObject<span class="token punctuation">;</span>
            <span class="token keyword">int</span> n <span class="token operator">=</span> value<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">==</span> anotherString<span class="token punctuation">.</span>value<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">char</span> v1<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> value<span class="token punctuation">;</span>
                <span class="token keyword">char</span> v2<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> anotherString<span class="token punctuation">.</span>value<span class="token punctuation">;</span>
                <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
                <span class="token comment">// &#x5224;&#x65AD;&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x7B26;&#x662F;&#x5426;&#x76F8;&#x7B49;</span>
                <span class="token keyword">while</span> <span class="token punctuation">(</span>n<span class="token operator">--</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>v1<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> v2<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
                        <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
                    i<span class="token operator">++</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="hashcode"><a name="hashcode" class="anchor-navigation-ex-anchor" href="#hashcode"><i class="fa fa-link" aria-hidden="true"></i></a>1.13.2. hashcode()</h3>
<blockquote>
<p>&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;hashCode()&#x76F8;&#x540C;&#xFF0C;&#x662F;&#x5426;equals()&#x76F8;&#x7B49;</p>
</blockquote>
<p>&#x4E0D;&#x540C;&#x7684;&#x5BF9;&#x8C61;&#x53EF;&#x80FD;&#x4F1A;&#x751F;&#x6210;&#x76F8;&#x540C;&#x7684;hashcode&#x503C;&#xFF0C;&#x56E0;&#x6B64;hashcode&#x503C;&#x76F8;&#x540C;&#x65E0;&#x6CD5;&#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x76F8;&#x7B49;&#x3002;</p>
<p>&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;hashcode&#x503C;&#x4E0D;&#x7B49;&#xFF0C;&#x5219;&#x5FC5;&#x5B9A;&#x662F;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x5BF9;&#x8C61;&#x3002;&#x5982;&#x679C;&#x8981;&#x5224;&#x5B9A;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x76F8;&#x7B49;&#xFF0C;&#x5FC5;&#x987B;&#x901A;&#x8FC7;equals&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;equals&#x4E3A;false &lt;=&gt; &#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;hashcode&#x503C;&#x4E0D;&#x7B49;</p>
<p>&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;equals&#x4E3A;true =&gt; &#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;hashcode&#x503C;&#x76F8;&#x7B49;</p>
<p>&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;hashcode&#x503C;&#x76F8;&#x7B49;&#xFF0C;equals&#x65B9;&#x6CD5;&#x5F97;&#x5230;&#x7684;&#x7ED3;&#x679C;&#x672A;&#x77E5;</p>
<blockquote>
<p>hashCode</p>
</blockquote>
<p>hasCode&#x65B9;&#x6CD5;&#x7684;&#x4E3B;&#x8981;&#x4F5C;&#x7528;&#x662F;&#x4E3A;&#x4E86;&#x914D;&#x5408;&#x57FA;&#x4E8E;&#x6563;&#x5217;&#x7684;&#x96C6;&#x5408;&#x4E00;&#x8D77;&#x6B63;&#x5E38;&#x4F7F;&#x7528;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x6563;&#x5217;&#x96C6;&#x5408;&#x5305;&#x62EC;HashSet&#x3001;HashMap&#x3001;HashTable&#xFF0C;&#x6216;&#x8005;&#x8BF4;&#x662F;&#x89E3;&#x51B3;&#x4E86;&#x5411;&#x96C6;&#x5408;&#x4E2D;&#x63D2;&#x5165;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x5224;&#x65AD;&#x5728;&#x96C6;&#x5408;&#x4E2D;&#x662F;&#x5426;&#x5B58;&#x5728;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x6B64;&#x7C7B;&#x60C5;&#x51B5;&#x4E0D;&#x9002;&#x5408;&#x4F7F;&#x7528;equals&#xFF0C;&#x901A;&#x4FD7;&#x5730;&#x8BB2;&#x5C31;&#x662F;Java&#x4E2D;&#x7684;hashCode&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x6839;&#x636E;&#x4E00;&#x5B9A;&#x7684;&#x89C4;&#x5219;&#x4E0E;&#x5BF9;&#x8C61;&#x76F8;&#x5173;&#x7684;&#x4FE1;&#x606F;&#xFF08;&#x6BD4;&#x5982;&#x5BF9;&#x8C61;&#x7684;&#x5B58;&#x50A8;&#x5730;&#x5740;&#xFF0C;&#x5BF9;&#x8C61;&#x7684;&#x5B57;&#x6BB5;&#x7B49;&#xFF09;&#x6620;&#x5C04;&#x6210;&#x4E00;&#x4E2A;&#x6570;&#x503C;&#xFF0C;&#x8FD9;&#x4E2A;&#x6570;&#x503C;&#x79F0;&#x4F5C;&#x6563;&#x5217;&#x503C;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">/*
HashMap.put&#x601D;&#x8DEF;
1.&#x8C03;&#x7528;hashCode&#x65B9;&#x6CD5;&#x5F97;&#x5230;&#x5143;&#x7D20;&#x7684;hashCode&#x503C;
2.&#x67E5;&#x770B;table&#x4E2D;&#x662F;&#x5426;&#x5B58;&#x5728;&#x8BE5;hashCode&#x503C;
3.&#x5B58;&#x5728;hashCode&#x503C;&#x4E14;equals&#x5224;&#x5B9A;&#x5143;&#x7D20;&#x5B58;&#x5728;&#xFF0C;&#x66F4;&#x65B0;&#x5143;&#x7D20;
4.&#x5426;&#x5219;&#x5C06;&#x5143;&#x7D20;&#x6DFB;&#x52A0;&#x5230;HashMap&#x4E2D;
*/</span>
<span class="token keyword">package</span> <span class="token namespace">java<span class="token punctuation">.</span>util</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">Serializable</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">K</span><span class="token punctuation">,</span> <span class="token class-name">V</span><span class="token punctuation">&gt;</span></span>
        <span class="token keyword">extends</span> <span class="token class-name">AbstractMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">K</span><span class="token punctuation">,</span> <span class="token class-name">V</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">implements</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">K</span><span class="token punctuation">,</span> <span class="token class-name">V</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">,</span> <span class="token class-name">Cloneable</span><span class="token punctuation">,</span> <span class="token class-name">Serializable</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token class-name">V</span> <span class="token function">put</span><span class="token punctuation">(</span><span class="token class-name">K</span> key<span class="token punctuation">,</span> <span class="token class-name">V</span> value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>key <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">putForNullKey</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> hash <span class="token operator">=</span> <span class="token function">hash</span><span class="token punctuation">(</span>key<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token function">indexFor</span><span class="token punctuation">(</span>hash<span class="token punctuation">,</span> table<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Entry</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">K</span><span class="token punctuation">,</span> <span class="token class-name">V</span><span class="token punctuation">&gt;</span></span> e <span class="token operator">=</span> table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> e <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> e <span class="token operator">=</span> e<span class="token punctuation">.</span>next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">Object</span> k<span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>e<span class="token punctuation">.</span>hash <span class="token operator">==</span> hash <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>k <span class="token operator">=</span> e<span class="token punctuation">.</span>key<span class="token punctuation">)</span> <span class="token operator">==</span> key <span class="token operator">||</span> key<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">V</span> oldValue <span class="token operator">=</span> e<span class="token punctuation">.</span>value<span class="token punctuation">;</span>
                e<span class="token punctuation">.</span>value <span class="token operator">=</span> value<span class="token punctuation">;</span>
                e<span class="token punctuation">.</span><span class="token function">recordAccess</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> oldValue<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        modCount<span class="token operator">++</span><span class="token punctuation">;</span>
        <span class="token function">addEntry</span><span class="token punctuation">(</span>hash<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>hashcode&#x8BBE;&#x8BA1;&#x8981;&#x5219;</p>
</blockquote>
<p>&#x65E0;&#x8BBA;&#x4F55;&#x65F6;&#xFF0C;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x8C03;&#x7528;hashcode()&#x90FD;&#x5E94;&#x8BE5;&#x4EA7;&#x751F;&#x540C;&#x6837;&#x7684;&#x503C;</p>
<p>&#x5982;&#x679C;&#x5BF9;&#x8C61;put()&#x65F6;&#x4EA7;&#x751F;&#x7684;hashcode&#x503C;&#x4E0E;get()&#x65F6;&#x53D6;&#x51FA;&#x7684;hashcode&#x503C;&#x4E0D;&#x540C;&#xFF0C;&#x90A3;&#x4E48;&#x5C06;&#x65E0;&#x6CD5;&#x83B7;&#x53D6;&#x8BE5;&#x5BF9;&#x8C61;</p>
<p>&#x56E0;&#x6B64;hashcode&#x65B9;&#x6CD5;&#x4F9D;&#x8D56;&#x4E8E;&#x5BF9;&#x8C61;&#x4E2D;&#x6613;&#x53D8;&#x7684;&#x6570;&#x636E;&#x4E14;&#x6570;&#x636E;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#xFF0C;hashcode()&#x65B9;&#x6CD5;&#x4F1A;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x6563;&#x5217;&#x503C;</p>
<p><a href="https://www.cnblogs.com/dolphin0520/p/3681042.html" target="_blank">&#x6D45;&#x8C08;Java&#x4E2D;&#x7684;hashcode&#x65B9;&#x6CD5; - Matrix&#x6D77;&#x5B50; - &#x535A;&#x5BA2;&#x56ED; (cnblogs.com)</a></p>
<h3 id="tostring"><a name="tostring" class="anchor-navigation-ex-anchor" href="#tostring"><i class="fa fa-link" aria-hidden="true"></i></a>1.13.3. toString()</h3>
<p>&#x9ED8;&#x8BA4;&#x8FD4;&#x56DE;Test@39a054a5&#x8FD9;&#x79CD;&#x5F62;&#x5F0F;&#xFF0C;@&#x540E;&#x9762;&#x4E3A;&#x6563;&#x5217;&#x503C;&#xFF08;hashcode&#xFF09;&#x7684;&#x65E0;&#x7B26;&#x53F7;&#x5341;&#x516D;&#x8FDB;&#x5236;&#x8868;&#x793A;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>test<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>test<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span>
                <span class="token string">&quot;@&quot;</span> <span class="token operator">+</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">toHexString</span><span class="token punctuation">(</span>test<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>test<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="clone"><a name="clone" class="anchor-navigation-ex-anchor" href="#clone"><i class="fa fa-link" aria-hidden="true"></i></a>1.13.4. clone()</h3>
<ol>
<li>clonable</li>
</ol>
<p>clone()&#x662F;Object&#x7684;protected&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x662F;public&#xFF0C;&#x9700;&#x8981;&#x663E;&#x793A;&#x91CD;&#x5199;clone()&#x4E14;&#x5B9E;&#x73B0;Cloneable&#x63A5;&#x53E3;&#x540E;&#x624D;&#x5141;&#x8BB8;&#x8C03;&#x7528;</p>
<ol>
<li>&#x6D45;&#x62F7;&#x8D1D;</li>
</ol>
<p>&#x62F7;&#x8D1D;&#x5BF9;&#x8C61;&#x548C;&#x539F;&#x59CB;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x5F15;&#x7528;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;</p>
<ol>
<li>&#x6DF1;&#x62F7;&#x8D1D;</li>
</ol>
<p>&#x62F7;&#x8D1D;&#x5BF9;&#x8C61;&#x548C;&#x539F;&#x59CB;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x5F15;&#x7528;&#x4E0D;&#x540C;&#x5BF9;&#x8C61;</p>
<ol>
<li>clone()&#x66FF;&#x4EE3;&#x65B9;&#x6848;</li>
</ol>
<p>clone()&#x65B9;&#x6CD5;&#x62F7;&#x8D1D;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x53CA;&#x590D;&#x6742;&#x53C8;&#x6709;&#x98CE;&#x9669;&#xFF0C;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x5E76;&#x4E14;&#x8FD8;&#x8981;&#x7C7B;&#x578B;&#x8F6C;&#x6362;</p>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x62F7;&#x8D1D;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6216;&#x62F7;&#x8D1D;&#x5DE5;&#x5382;&#x6765;&#x62F7;&#x8D1D;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;</p>
<h1 id="&#x591A;&#x7EBF;&#x7A0B;"><a name="&#x591A;&#x7EBF;&#x7A0B;" class="anchor-navigation-ex-anchor" href="#&#x591A;&#x7EBF;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a>2. &#x591A;&#x7EBF;&#x7A0B;</h1>
<ul>
<li>&#x539F;&#x5B50;&#x6027;</li>
</ul>
<p>&#x5B9A;&#x4E49;&#xFF1A;&#x4E00;&#x4E2A;&#x64CD;&#x4F5C;&#x6216;&#x8005;&#x662F;&#x591A;&#x4E2A;&#x64CD;&#x4F5C;&#xFF0C;&#x8981;&#x4E48;&#x5168;&#x90E8;&#x6267;&#x884C;&#x5E76;&#x4E14;&#x6267;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4E0D;&#x88AB;&#x4EFB;&#x4F55;&#x56E0;&#x7D20;&#x6253;&#x65AD;&#xFF0C;&#x8981;&#x4E48;&#x5C31;&#x90FD;&#x4E0D;&#x6267;&#x884C;</p>
<p>&#x539F;&#x5B50;&#x6027;&#x662F;&#x62D2;&#x7EDD;&#x591A;&#x7EBF;&#x7A0B;&#x64CD;&#x4F5C;&#x7684;&#xFF0C;&#x5728;&#x6574;&#x4E2A;&#x64CD;&#x4F5C;&#x8FC7;&#x7A0B;&#x4E2D;&#x4E0D;&#x4F1A;&#x88AB;&#x7EBF;&#x7A0B;&#x8C03;&#x5EA6;&#x5668;&#x4E2D;&#x65AD;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x90FD;&#x53EF;&#x8BA4;&#x4E3A;&#x662F;&#x539F;&#x5B50;&#x6027;</p>
<p>&#xFF08;1&#xFF09;&#x57FA;&#x672C;&#x7C7B;&#x578B;&#x7684;&#x8BFB;&#x53D6;&#x548C;&#x8D4B;&#x503C;&#x64CD;&#x4F5C;&#xFF0C;&#x5207;&#x8D4B;&#x503C;&#x5FC5;&#x987B;&#x662F;&#x8D4B;&#x503C;&#x7ED9;&#x53D8;&#x91CF;&#xFF0C;&#x53D8;&#x91CF;&#x4E4B;&#x95F4;&#x7684;&#x76F8;&#x4E92;&#x8D4B;&#x503C;&#x4E0D;&#x662F;&#x539F;&#x5B50;&#x64CD;&#x4F5C;</p>
<p>&#xFF08;2&#xFF09;&#x6240;&#x6709;&#x5F15;&#x7528;&#x7684;reference&#x7684;&#x8D4B;&#x503C;&#x64CD;&#x4F5C;</p>
<p>&#xFF08;3&#xFF09;java.concurrent.Atomic.*&#x5305;&#x4E2D;&#x6240;&#x6709;&#x7C7B;&#x7684;&#x4E00;&#x5207;&#x64CD;&#x4F5C;</p>
<ul>
<li>&#x53EF;&#x89C1;&#x6027;</li>
</ul>
<p>&#x5B9A;&#x4E49;&#xFF1A;&#x6307;&#x5F53;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x540C;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4FEE;&#x6539;&#x4E86;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x503C;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x80FD;&#x591F;&#x7ACB;&#x5373;&#x770B;&#x5F97;&#x5230;&#x4FEE;&#x6539;&#x7684;&#x503C;</p>
<p>&#x5728;&#x591A;&#x7EBF;&#x7A0B;&#x73AF;&#x5883;&#x4E0B;&#xFF0C;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5BF9;&#x5171;&#x4EAB;&#x53D8;&#x91CF;&#x7684;&#x64CD;&#x4F5C;&#x5BF9;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x662F;&#x4E0D;&#x53EF;&#x89C1;&#x7684;&#x3002;Java&#x63D0;&#x4F9B;&#x4E86;volatile&#x6765;&#x4FDD;&#x8BC1;&#x53EF;&#x89C1;&#x6027;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;&#x88AB;volatile&#x4FEE;&#x9970;&#x540E;&#xFF0C;&#x8868;&#x793A;&#x7740;&#x7EBF;&#x7A0B;&#x672C;&#x5730;&#x5185;&#x5B58;&#x65E0;&#x6548;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4FEE;&#x6539;&#x5171;&#x4EAB;&#x53D8;&#x91CF;&#x540E;&#x4ED6;&#x4F1A;&#x7ACB;&#x5373;&#x88AB;&#x66F4;&#x65B0;&#x5230;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x8BFB;&#x53D6;&#x5171;&#x4EAB;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x4F1A;&#x76F4;&#x63A5;&#x4ECE;&#x4E3B;&#x5185;&#x5B58;&#x4E2D;&#x8BFB;&#x53D6;&#x3002;&#x5F53;&#x7136;&#xFF0C;synchronized&#x548C;Lock&#x90FD;&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x53EF;&#x89C1;&#x6027;&#x3002;synchronized&#x548C;Lock&#x80FD;&#x4FDD;&#x8BC1;&#x540C;&#x4E00;&#x65F6;&#x523B;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x83B7;&#x53D6;&#x9501;&#x7136;&#x540E;&#x6267;&#x884C;&#x540C;&#x6B65;&#x4EE3;&#x7801;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x91CA;&#x653E;&#x9501;&#x4E4B;&#x524D;&#x4F1A;&#x5C06;&#x5BF9;&#x53D8;&#x91CF;&#x7684;&#x4FEE;&#x6539;&#x5237;&#x65B0;&#x5230;&#x4E3B;&#x5185;&#x5B58;&#x4E2D;&#x3002;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x53EF;&#x89C1;&#x6027;</p>
<ul>
<li>&#x6709;&#x5E8F;&#x6027;</li>
</ul>
<p>&#x5B9A;&#x4E49;&#xFF1A;&#x5373;&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x7684;&#x987A;&#x5E8F;&#x6309;&#x7167;&#x4EE3;&#x7801;&#x7684;&#x5148;&#x540E;&#x987A;&#x5E8F;&#x6267;&#x884C;</p>
<p>Java&#x5185;&#x5B58;&#x6A21;&#x578B;&#x4E2D;&#x7684;&#x6709;&#x5E8F;&#x6027;&#x53EF;&#x4EE5;&#x603B;&#x7ED3;&#x4E3A;&#xFF1A;&#x5728;&#x672C;&#x7EBF;&#x7A0B;&#x5185;&#x89C2;&#x5BDF;&#xFF0C;&#x6240;&#x6709;&#x64CD;&#x4F5C;&#x662F;&#x6709;&#x5E8F;&#x7684;&#xFF1B;&#x5982;&#x679C;&#x5728;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E2D;&#x89C2;&#x5BDF;&#x53E6;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#xFF0C;&#x6240;&#x6709;&#x64CD;&#x4F5C;&#x90FD;&#x662F;&#x65E0;&#x5E8F;&#x7684;&#x3002;&#x524D;&#x534A;&#x53E5;&#x662F;&#x6307;&#x201C;&#x7EBF;&#x7A0B;&#x5185;&#x6807;&#x5FC3;&#x554A;&#x4E3A;&#x4E32;&#x884C;&#x8BED;&#x4E49;&#x201D;&#xFF0C;&#x540E;&#x534A;&#x53E5;&#x662F;&#x6307;&#x201C;&#x6307;&#x4EE4;&#x91CD;&#x6392;&#x7A0B;&#x5E8F;&#x201D;&#x73B0;&#x8C61;&#x548C;&#x201C;&#x5DE5;&#x4F5C;&#x5185;&#x5B58;&#x4E0E;&#x4E3B;&#x5185;&#x5B58;&#x540C;&#x6B65;&#x5EF6;&#x8FDF;&#x201D;&#x73B0;&#x8C61;</p>
<p>&#x5728;Java&#x5185;&#x5B58;&#x6A21;&#x578B;&#x4E2D;&#xFF0C;&#x4E3A;&#x4E86;&#x6548;&#x7387;&#x5141;&#x8BB8;&#x7F16;&#x8BD1;&#x5668;&#x548C;&#x5904;&#x7406;&#x5668;&#x5BF9;&#x6307;&#x4EE4;&#x8FDB;&#x884C;&#x91CD;&#x6392;&#x5E8F;&#xFF0C;&#x5F53;&#x7136;&#x91CD;&#x6392;&#x5E8F;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x5355;&#x7EBF;&#x7A0B;&#x7684;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF0C;&#x4F46;&#x662F;&#x5BF9;&#x591A;&#x7EBF;&#x7A0B;&#x6709;&#x5F71;&#x54CD;&#x3002;Java&#x63D0;&#x4F9B;volatile&#x6765;&#x4FDD;&#x8BC1;&#x4E00;&#x5B9A;&#x7684;&#x6709;&#x5E8F;&#x6027;&#x3002;&#x53E6;&#x5916;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;synchronized&#x548C;Lock&#x6765;&#x4FDD;&#x8BC1;&#x6709;&#x5E8F;&#x6027;&#xFF0C;synchronized&#x548C;Lock&#x4FDD;&#x8BC1;&#x6BCF;&#x4E2A;&#x65F6;&#x523B;&#x662F;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x540C;&#x6B65;&#x4EE3;&#x7801;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;&#x8BA9;&#x7EBF;&#x7A0B;&#x987A;&#x5E8F;&#x6267;&#x884C;&#x540C;&#x6B65;&#x4EE3;&#x7801;&#xFF0C;&#x81EA;&#x7136;&#x4FDD;&#x8BC1;&#x4E86;&#x6709;&#x5E8F;&#x6027;</p>
<h2 id="&#x5185;&#x5B58;&#x6A21;&#x578B;"><a name="&#x5185;&#x5B58;&#x6A21;&#x578B;" class="anchor-navigation-ex-anchor" href="#&#x5185;&#x5B58;&#x6A21;&#x578B;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1. &#x5185;&#x5B58;&#x6A21;&#x578B;</h2>
<h2 id="&#x8FDB;&#x7A0B;&#x4E0E;&#x7EBF;&#x7A0B;"><a name="&#x8FDB;&#x7A0B;&#x4E0E;&#x7EBF;&#x7A0B;" class="anchor-navigation-ex-anchor" href="#&#x8FDB;&#x7A0B;&#x4E0E;&#x7EBF;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2. &#x8FDB;&#x7A0B;&#x4E0E;&#x7EBF;&#x7A0B;</h2>
<p>&#x8FDB;&#x7A0B;process&#x662F;&#x8BA1;&#x7B97;&#x673A;&#x4E2D;&#x7684;&#x7A0B;&#x5E8F;&#x5173;&#x4E8E;&#x67D0;&#x6570;&#x636E;&#x96C6;&#x5408;&#x4E0A;&#x7684;&#x4E00;&#x6B21;&#x8FD0;&#x884C;&#x6D3B;&#x52A8;&#xFF0C;&#x662F;&#x7CFB;&#x7EDF;&#x8FDB;&#x884C;&#x8D44;&#x6E90;&#x5206;&#x914D;&#x548C;&#x8C03;&#x5EA6;&#x7684;&#x57FA;&#x672C;&#x5355;&#x4F4D;&#xFF0C;&#x662F;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x7ED3;&#x6784;&#x7684;&#x57FA;&#x7840;&#x3002;&#x5728;&#x65E9;&#x671F;&#xFF0C;&#x8FDB;&#x7A0B;&#x662F;&#x7A0B;&#x5E8F;&#x7684;&#x57FA;&#x672C;&#x6267;&#x884C;&#x5B9E;&#x4F53;&#xFF0C;&#x5F53;&#x4EE3;&#x8BA1;&#x7B97;&#x673A;&#x7ED3;&#x6784;&#x4E2D;&#xFF0C;&#x8FDB;&#x7A0B;&#x662F;&#x7EBF;&#x7A0B;&#x7684;&#x5BB9;&#x5668;&#x3002;&#x7A0B;&#x5E8F;&#x662F;&#x6307;&#x4EE4;&#x3001;&#x6570;&#x636E;&#x53CA;&#x5176;&#x7EC4;&#x7EC7;&#x5F62;&#x5F0F;&#x7684;&#x63CF;&#x8FF0;&#xFF0C;&#x8FDB;&#x7A0B;&#x662F;&#x7A0B;&#x5E8F;&#x7684;&#x5B9E;&#x4F53;&#x3002;</p>
<p>&#x7EBF;&#x7A0B;&#x662F;&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x6D41;&#x7684;&#x6700;&#x5C0F;&#x5355;&#x5143;&#x3002;&#x7EBF;&#x7A0B;&#x662F;&#x7A0B;&#x5E8F;&#x4E2D;&#x4E00;&#x4E2A;&#x5355;&#x4E00;&#x7684;&#x987A;&#x5E8F;&#x63A7;&#x5236;&#x6D41;&#x7A0B;&#x3002;&#x8FDB;&#x7A0B;&#x5185;&#x4E00;&#x4E2A;&#x76F8;&#x5BF9;&#x72EC;&#x7ACB;&#x7684;&#x3001;&#x53EF;&#x8C03;&#x5EA6;&#x7684;&#x6267;&#x884C;&#x5355;&#x5143;&#xFF0C;&#x662F;&#x7CFB;&#x7EDF;&#x72EC;&#x7ACB;&#x8C03;&#x5EA6;&#x548C;&#x5206;&#x914D;CPU&#x7684;&#x57FA;&#x672C;&#x5355;&#x4F4D;&#x6307;&#x8FD0;&#x884C;&#x4E2D;&#x7684;&#x7A0B;&#x5E8F;&#x7684;&#x8C03;&#x5EA6;&#x5355;&#x4F4D;&#x3002;&#x5728;&#x5355;&#x4E2A;&#x7A0B;&#x5E8F;&#x4E2D;&#x540C;&#x65F6;&#x8FD0;&#x884C;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x4E0D;&#x540C;&#x7684;&#x5DE5;&#x4F5C;&#xFF0C;&#x6210;&#x4E3A;&#x591A;&#x7EBF;&#x7A0B;&#x3002;</p>
<blockquote>
<p>&#x7EBF;&#x7A0B;&#x548C;&#x8FDB;&#x7A0B;&#x7684;&#x533A;&#x522B;</p>
</blockquote>
<ul>
<li>&#x8FDB;&#x7A0B;&#x662F;&#x8D44;&#x6E90;&#x5206;&#x914D;&#x7684;&#x6700;&#x5C0F;&#x5355;&#x4F4D;&#xFF0C;&#x7EBF;&#x7A0B;&#x662F;&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x7684;&#x6700;&#x5C0F;&#x5355;&#x4F4D;
-
&#x8FDB;&#x884C;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x72EC;&#x7ACB;&#x5730;&#x5740;&#x7A7A;&#x95F4;&#xFF0C;&#x6BCF;&#x542F;&#x52A8;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#xFF0C;&#x7CFB;&#x7EDF;&#x5C31;&#x4F1A;&#x4E3A;&#x5B83;&#x5206;&#x914D;&#x5730;&#x5740;&#x7A7A;&#x95F4;&#xFF0C;&#x5EFA;&#x7ACB;&#x6570;&#x636E;&#x8868;&#x6765;&#x7EF4;&#x62A4;&#x4EE3;&#x7801;&#x6BB5;&#x3001;&#x5806;&#x6808;&#x6BB5;&#x548C;&#x6570;&#x636E;&#x6BB5;&#xFF0C;&#x8FD9;&#x79CD;&#x64CD;&#x4F5C;&#x975E;&#x5E38;&#x6602;&#x8D35;&#x3002;&#x7EBF;&#x7A0B;&#x662F;&#x5171;&#x4EAB;&#x8FDB;&#x7A0B;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x7684;&#xFF0C;&#x4F7F;&#x7528;&#x76F8;&#x540C;&#x7684;&#x5730;&#x5740;&#x7A7A;&#x95F4;&#xFF0C;&#x56E0;&#x6B64;CPU&#x5207;&#x6362;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x7684;&#x82B1;&#x8D39;&#x8FDC;&#x6BD4;&#x8FDB;&#x7A0B;&#x8981;&#x5C0F;&#x5F88;&#x591A;&#xFF0C;&#x540C;&#x65F6;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x7684;&#x5F00;&#x9500;&#x4E5F;&#x6BD4;&#x8FDB;&#x7A0B;&#x8981;&#x5C0F;&#x5F88;&#x591A;&#xFF0C;&#x7EBF;&#x7A0B;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x5207;&#x6362;&#x7684;&#x6027;&#x80FD;&#x6D88;&#x8017;&#x8981;&#x5C0F;&#x4E8E;&#x8FDB;&#x7A0B;</li>
<li>&#x7EBF;&#x7A0B;&#x4E4B;&#x95F4;&#x7684;&#x901A;&#x4FE1;&#x66F4;&#x65B9;&#x4FBF;&#xFF0C;&#x540C;&#x4E00;&#x8FDB;&#x7A0B;&#x4E0B;&#x7684;&#x7EBF;&#x7A0B;&#x5171;&#x4EAB;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x3001;&#x9759;&#x6001;&#x53D8;&#x91CF;&#x7B49;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x8FDB;&#x7A0B;&#x4E4B;&#x95F4;&#x7684;&#x901A;&#x4FE1;&#x9700;&#x8981;&#x4EE5;&#x901A;&#x4FE1;&#x7684;&#x65B9;&#x5F0F;IPC&#x8FDB;&#x884C;&#x3002;&#x5982;&#x4F55;&#x5904;&#x7406;&#x597D;&#x540C;&#x6B65;&#x4E0E;&#x4E92;&#x65A5;&#x662F;&#x7F16;&#x5199;&#x591A;&#x7EBF;&#x7A0B;&#x7A0B;&#x5E8F;&#x7684;&#x96BE;&#x70B9;</li>
<li>&#x591A;&#x8FDB;&#x7A0B;&#x7A0B;&#x5E8F;&#x66F4;&#x5065;&#x58EE;&#xFF0C;&#x591A;&#x7EBF;&#x7A0B;&#x7A0B;&#x5E8F;&#x53EA;&#x8981;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6B7B;&#x6389;&#xFF0C;&#x6574;&#x4E2A;&#x8FDB;&#x7A0B;&#x4E5F;&#x6B7B;&#x6389;&#x4E86;&#xFF0C;&#x800C;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x6B7B;&#x6389;&#x5E76;&#x4E0D;&#x4F1A;&#x5BF9;&#x53E6;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x9020;&#x6210;&#x5F71;&#x54CD;&#xFF0C;&#x56E0;&#x4E3A;&#x8FDB;&#x7A0B;&#x6709;&#x81EA;&#x5DF1;&#x72EC;&#x7ACB;&#x7684;&#x5730;&#x5740;&#x7A7A;&#x95F4;</li>
</ul>
<h2 id="&#x540C;&#x6B65;&#x4E0E;&#x5F02;&#x6B65;"><a name="&#x540C;&#x6B65;&#x4E0E;&#x5F02;&#x6B65;" class="anchor-navigation-ex-anchor" href="#&#x540C;&#x6B65;&#x4E0E;&#x5F02;&#x6B65;"><i class="fa fa-link" aria-hidden="true"></i></a>2.3. &#x540C;&#x6B65;&#x4E0E;&#x5F02;&#x6B65;</h2>
<p>&#x540C;&#x6B65;&#xFF1A;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x540E;&#x5FC5;&#x987B;&#x7B49;&#x5F85;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#xFF0C;&#x624D;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x540E;&#x7EED;&#x7684;&#x65B9;&#x6CD5;</p>
<p>&#x5F02;&#x6B65;&#xFF1A;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x540E;&#x7ACB;&#x5373;&#x8FD4;&#x56DE;&#x5E76;&#x6267;&#x884C;&#x540E;&#x7EED;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5F02;&#x6B65;&#x65B9;&#x6CD5;&#x4F1A;&#x542F;&#x52A8;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x53BB;&#x8FD0;&#x884C;</p>
<h2 id="&#x5E76;&#x53D1;&#x548C;&#x5E76;&#x884C;"><a name="&#x5E76;&#x53D1;&#x548C;&#x5E76;&#x884C;" class="anchor-navigation-ex-anchor" href="#&#x5E76;&#x53D1;&#x548C;&#x5E76;&#x884C;"><i class="fa fa-link" aria-hidden="true"></i></a>2.4. &#x5E76;&#x53D1;&#x548C;&#x5E76;&#x884C;</h2>
<p>&#x5E76;&#x884C;&#x6307;&#x7684;&#x662F;&#x540C;&#x4E00;&#x65F6;&#x523B;&#x6709;&#x591A;&#x4E2A;&#x4EFB;&#x52A1;&#x540C;&#x65F6;&#x6267;&#x884C;&#x4E14;&#x4E92;&#x4E0D;&#x5F71;&#x54CD;</p>
<p>&#x5E76;&#x53D1;&#x6307;&#x7684;&#x662F;&#x540C;&#x4E00;&#x65F6;&#x95F4;&#x6BB5;&#x6709;&#x591A;&#x4E2A;&#x4EFB;&#x52A1;&#x201C;&#x540C;&#x65F6;&#x201D;&#x6267;&#x884C;&#xFF0C;&#x4F46;&#x662F;&#x540C;&#x4E00;&#x65F6;&#x523B;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x8FD0;&#x884C;&#xFF0C;&#x5148;&#x540E;&#x4EA4;&#x66FF;&#x8FD0;&#x884C;</p>
<h2 id="&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;"><a name="&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;" class="anchor-navigation-ex-anchor" href="#&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;"><i class="fa fa-link" aria-hidden="true"></i></a>2.5. &#x5B9E;&#x73B0;&#x65B9;&#x5F0F;</h2>
<p>&#xFF08;1&#xFF09;&#x7EE7;&#x627F;Thread</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ThreadTest</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">100</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ThreadTest</span> threadTest <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        threadTest<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#xFF08;2&#xFF09;&#x5B9E;&#x73B0;Runable/callable</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ThreadTest</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;aaa&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ThreadTest</span> threadTest <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>threadTest<span class="token punctuation">,</span> <span class="token string">&quot;t1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>Thread&#x7C7B;&#x5B9E;&#x73B0;&#x4E86;Runnable&#x63A5;&#x53E3;&#xFF0C;&#x4F46;&#x662F;run&#x65B9;&#x6CD5;&#x4E3B;&#x4F53;&#x662F;&#x7A7A;&#x7684;</p>
<p>Thread&#x7C7B;&#x7684;run&#x65B9;&#x6CD5;&#x901A;&#x5E38;&#x7531;&#x5B50;&#x7C7B;run&#x65B9;&#x6CD5;&#x91CD;&#x5199;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">abstract</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Thread</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="&#x7EBF;&#x7A0B;&#x5B89;&#x5168;"><a name="&#x7EBF;&#x7A0B;&#x5B89;&#x5168;" class="anchor-navigation-ex-anchor" href="#&#x7EBF;&#x7A0B;&#x5B89;&#x5168;"><i class="fa fa-link" aria-hidden="true"></i></a>2.6. &#x7EBF;&#x7A0B;&#x5B89;&#x5168;</h2>
<p>&#x5F53;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x67D0;&#x4E00;&#x4E2A;&#x7C7B;&#xFF08;&#x5BF9;&#x8C61;&#x6216;&#x65B9;&#x6CD5;&#xFF09;&#x65F6;&#xFF0C;&#x8FD9;&#x4E2A;&#x7C7B;&#x59CB;&#x7EC8;&#x80FD;&#x8868;&#x73B0;&#x51FA;&#x6B63;&#x786E;&#x7684;&#x884C;&#x4E3A;&#xFF0C;&#x5C31;&#x662F;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x7684;</p>
<p>&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x5C31;&#x662F;&#x591A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x65F6;&#xFF0C;&#x91C7;&#x7528;&#x4E86;&#x52A0;&#x9501;&#x673A;&#x5236;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x8BE5;&#x7C7B;&#x7684;&#x67D0;&#x4E2A;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x8FDB;&#x884C;&#x4FDD;&#x62A4;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x4E0D;&#x80FD;&#x8FDB;&#x884C;&#x8BBF;&#x95EE;&#xFF0C;&#x76F4;&#x5230;&#x8BE5;&#x7EBF;&#x7A0B;&#x8BFB;&#x53D6;&#x5B8C;&#xFF0C;&#x91CA;&#x653E;&#x4E86;&#x9501;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x624D;&#x53EF;&#x7528;&#xFF0C;&#x8FD9;&#x6837;&#x4FDD;&#x8BC1;&#x4E86;&#x6570;&#x636E;&#x7684;&#x4E00;&#x81F4;&#x6027;</p>
<p>&#x6570;&#x636E;&#x4E0D;&#x5B89;&#x5168;&#x5C31;&#x662F;&#x4E0D;&#x63D0;&#x4F9B;&#x6570;&#x636E;&#x8BBF;&#x95EE;&#x4FDD;&#x62A4;&#xFF0C;&#x6709;&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x5148;&#x540E;&#x66F4;&#x6539;&#x6570;&#x636E;&#x4EE5;&#x81F3;&#x4E8E;&#x6240;&#x5F97;&#x5230;&#x7684;&#x6570;&#x636E;&#x662F;&#x810F;&#x6570;&#x636E;</p>
<p>&#x540C;&#x6B65;synchronized&#x4FDD;&#x8BC1;&#x4E86;&#x7EBF;&#x7A0B;&#x7684;&#x5B89;&#x5168;&#xFF0C;&#x9700;&#x8981;&#x6EE1;&#x8DB3;&#x4E24;&#x4E2A;&#x6700;&#x57FA;&#x672C;&#x7684;&#x7279;&#x6027;&#xFF1A;&#x539F;&#x5B50;&#x6027;&#x548C;&#x53EF;&#x89C1;&#x6027;</p>
<p>&#x5F02;&#x6B65;asynchronized&#x5C31;&#x662F;&#x72EC;&#x7ACB;&#xFF0C;&#x76F8;&#x4E92;&#x4E4B;&#x95F4;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x5173;&#x7CFB;&#x548C;&#x5F71;&#x54CD;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x4E0D;&#x4F1A;&#x51FA;&#x73B0;&#x5171;&#x4EAB;&#x8D44;&#x6E90;&#x7684;&#x7ADE;&#x4E89;</p>
<h2 id="synchronized"><a name="synchronized" class="anchor-navigation-ex-anchor" href="#synchronized"><i class="fa fa-link" aria-hidden="true"></i></a>2.7. synchronized</h2>
<p>synchronized&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x610F;&#x5BF9;&#x8C61;&#x53CA;&#x65B9;&#x6CD5;&#x4E0A;&#x52A0;&#x9501;&#xFF0C;&#x52A0;&#x9501;&#x7684;&#x4EE3;&#x7801;&#x6BB5;&#x6210;&#x4E3A;<strong>&#x4E92;&#x65A5;&#x533A;</strong>&#x6216;<strong>&#x4E34;&#x754C;&#x533A;</strong></p>
<blockquote>
<p>&#x591A;&#x7EBF;&#x7A0B;&#x4E0D;&#x52A0;&#x9501;</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ThreadTest</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        count<span class="token operator">--</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot; count:&quot;</span> <span class="token operator">+</span> count<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ThreadTest</span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;thread1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;thread2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;thread3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;thread4&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;thread5&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread1<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread2<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread3<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread4<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread5<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0D;&#x52A0;&#x9501;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x7531;&#x4E8E;&#x5BF9;&#x8C61;&#x5171;&#x4EAB;&#xFF0C;&#x5728;&#x7EBF;&#x7A0B;&#x62FF;&#x5230;&#x5BF9;&#x8C61;&#x540E;&#x6CA1;&#x6709;&#x6267;&#x884C;&#x81EA;&#x51CF;&#x5C31;&#x5207;&#x6362;&#x5230;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;</p>
<p>&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#x5C31;&#x4F1A;&#x51FA;&#x73B0;&#x4E24;&#x6B21;&#x81EA;&#x51CF;&#x8BA1;&#x7B97;&#x51FA;&#x4E00;&#x6837;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x8FD9;&#x5C31;&#x5BFC;&#x81F4;&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x51FA;&#x9519;</p>
<blockquote>
<p>&#x591A;&#x7EBF;&#x7A0B;&#x52A0;&#x9501;</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ThreadTest</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">synchronized</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        count<span class="token operator">--</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot; count:&quot;</span> <span class="token operator">+</span> count<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ThreadTest</span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;thread1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;thread2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;thread3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;thread4&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;thread5&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread1<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread2<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread3<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread4<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread5<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x65B9;&#x6CD5;&#x52A0;&#x9501;&#x540E;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x4EE3;&#x7801;&#x65F6;&#x90FD;&#x4F1A;&#x52A0;&#x9501;&#xFF0C;&#x540E;&#x9762;&#x7684;&#x7EBF;&#x7A0B;&#x4F1A;&#x88AB;&#x963B;&#x585E;</p>
<p>&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x5B8C;&#x540E;&#x91CA;&#x653E;&#x9501;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF0C;&#x5C31;&#x4FDD;&#x8BC1;&#x4E86;&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x7684;&#x6B63;&#x786E;&#x6027;</p>
<blockquote>
<p>&#x9501;&#x91CD;&#x5165;</p>
</blockquote>
<p>synchronized&#x5177;&#x6709;&#x9501;&#x91CD;&#x5165;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5728;&#x4F7F;&#x7528;synchronized&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x9501;&#x540E;&#xFF0C;&#x5728;&#x8BE5;&#x9501;&#x91CC;&#x6267;&#x884C;&#x4EE3;&#x7801;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x518D;&#x6B21;&#x8BF7;&#x6C42;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x9501;&#x65F6;&#x53EF;&#x4EE5;&#x518D;&#x6B21;&#x5F97;&#x5230;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x9501;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x5F53;&#x7EBF;&#x7A0B;&#x8BF7;&#x6C42;&#x4E00;&#x4E2A;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x6301;&#x6709;&#x7684;&#x5BF9;&#x8C61;&#x9501;&#x65F6;&#xFF0C;&#x8BE5;&#x7EBF;&#x7A0B;&#x4F1A;&#x963B;&#x585E;&#xFF0C;&#x5F53;&#x7EBF;&#x7A0B;&#x8BF7;&#x6C42;&#x81EA;&#x5DF1;&#x6301;&#x6709;&#x7684;&#x5BF9;&#x8C61;&#x9501;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x8BE5;&#x9501;&#x662F;&#x91CD;&#x5165;&#x9501;&#xFF0C;&#x8BF7;&#x6C42;&#x5C31;&#x4F1A;&#x6210;&#x529F;&#xFF0C;&#x5426;&#x5219;&#x963B;&#x585E;&#x3002;</p>
<p>&#x53EF;&#x91CD;&#x5165;&#x9501;&#x6700;&#x5927;&#x7684;&#x4F5C;&#x7528;&#x662F;<strong>&#x907F;&#x514D;&#x6B7B;&#x9501;</strong>&#x3002;&#x5982;&#x4FEE;&#x6539;&#x5BC6;&#x7801;&#x65F6;&#x9700;&#x8981;&#x9A8C;&#x8BC1;&#x5BC6;&#x7801;&#x7684;&#x5408;&#x6CD5;&#x6027;&#xFF0C;&#x5E76;&#x4E14;&#x4E24;&#x4E2A;&#x90FD;&#x5E94;&#x8BE5;&#x52A0;&#x9501;</p>
<p>&#x53EF;&#x91CD;&#x5165;&#x9501;&#x652F;&#x6301;&#x7EE7;&#x627F;&#x6027;</p>
<p>&#x9501;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x65F6;&#xFF0C;&#x81EA;&#x52A8;&#x91CA;&#x653E;&#x9501;</p>
<p>&#x5C06;&#x4EFB;&#x610F;&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x76D1;&#x89C6;&#x5668;monitor</p>
<p>&#x5355;&#x4F8B;&#x6A21;&#x5F0F;-&#x53CC;&#x91CD;&#x6821;&#x9A8C;&#x9501;</p>
<blockquote>
<p>volatile&#x4E0E;synchronized&#x7684;&#x533A;&#x522B;</p>
</blockquote>
<ol>
<li>&#x5173;&#x952E;&#x5B57;volatile&#x662F;&#x7EBF;&#x7A0B;&#x540C;&#x6B65;&#x7684;&#x8F7B;&#x91CF;&#x7EA7;&#x5B9E;&#x73B0;&#xFF0C;&#x6027;&#x80FD;&#x6BD4;synchronized&#x597D;&#xFF0C;&#x5E76;&#x4E14;volatile&#x53EA;&#x80FD;&#x4FEE;&#x9970;&#x53D8;&#x91CF;&#xFF0C;synchronized&#x53EF;&#x4EE5;&#x4FEE;&#x9970;&#x65B9;&#x6CD5;&#xFF0C;&#x4EE3;&#x7801;&#x5757;</li>
<li>&#x591A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;volatile&#x4E0D;&#x4F1A;&#x53D1;&#x751F;&#x963B;&#x585E;&#xFF0C;&#x800C;synchronized&#x4F1A;&#x53D1;&#x751F;&#x963B;&#x585E;</li>
<li>volatile&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x6570;&#x636E;&#x7684;&#x53EF;&#x89C1;&#x6027;&#xFF0C;&#x4F46;&#x4E0D;&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x539F;&#x5B50;&#x6027;&#xFF0C;&#x800C;synchronized&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x539F;&#x5B50;&#x6027;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x7B80;&#x4ECB;&#x4FDD;&#x8BC1;&#x53EF;&#x89C1;&#x6027;&#xFF0C;&#x56E0;&#x4E3A;&#x4ED6;&#x4F1A;&#x5C06;&#x79C1;&#x6709;&#x5185;&#x5B58;&#x548C;&#x516C;&#x5171;&#x5185;&#x5B58;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x505A;&#x540C;&#x6B65;</li>
<li>volatile&#x89E3;&#x51B3;&#x7684;&#x662F;&#x53D8;&#x91CF;&#x5728;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E4B;&#x95F4;&#x7684;&#x53EF;&#x89C1;&#x6027;&#xFF0C;&#x800C;synchronized&#x89E3;&#x51B3;&#x7684;&#x662F;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E4B;&#x95F4;&#x8BBF;&#x95EE;&#x8D44;&#x6E90;&#x7684;&#x540C;&#x6B65;&#x6027;</li>
</ol>
<blockquote>
<p>&#x5BF9;&#x8C61;&#x89E3;&#x6784;</p>
</blockquote>
<p>HotSpot&#x865A;&#x62DF;&#x673A;&#x7684;<a href="https://github.com/openjdk/jdk/blob/jdk8-b01/hotspot/src/share/vm/oops/markOop.hpp" target="_blank">jdk/markOop.hpp at jdk8-b01 &#xB7; openjdk/jdk &#xB7; GitHub</a>
&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x6CE8;&#x91CA;&#x7247;&#x6BB5;&#xFF0C;&#x63CF;&#x8FF0;&#x4E86;64bits&#x4E0B;mark-word&#x7684;&#x5B58;&#x50A8;&#x72B6;&#x6001;&#x3002;&#x5BF9;&#x8C61;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x5B58;&#x50A8;&#x7684;&#x5E03;&#x5C40;&#x53EF;&#x4EE5;&#x5206;&#x4E3A;&#x4E09;&#x5757;&#x533A;&#x57DF;&#xFF1A;&#x5BF9;&#x8C61;&#x5934;&#xFF08;Header&#xFF09;&#x3001;&#x5B9E;&#x4F8B;&#x6570;&#x636E;&#xFF08;Instance
Data&#xFF09;&#x548C;&#x5BF9;&#x5176;&#x586B;&#x5145;&#xFF08;Padding&#xFF09;</p>
<ul>
<li>mark-word&#xFF1A;&#x5BF9;&#x8C61;&#x6807;&#x8BB0;&#x5B57;&#x6BB5;&#x5360;4&#x4E2A;&#x5B57;&#x8282;&#xFF0C;&#x7528;&#x4E8E;&#x5B58;&#x50A8;&#x4E00;&#x4E9B;&#x5217;&#x7684;&#x6807;&#x8BB0;&#x4F4D;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;&#x54C8;&#x5E0C;&#x503C;&#x3001;&#x8F7B;&#x91CF;&#x7EA7;&#x9501;&#x7684;&#x6807;&#x8BB0;&#x4F4D;&#xFF0C;&#x504F;&#x5411;&#x9501;&#x6807;&#x8BB0;&#x4F4D;&#x3001;&#x5206;&#x4EE3;&#x5E74;&#x9F84;&#x7B49;</li>
<li>Klass-Pointer&#xFF1A;Class&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#x9547;&#xFF0C;jdk1.8&#x9ED8;&#x8BA4;&#x5F00;&#x542F;&#x6307;&#x9488;&#x538B;&#x7F29;&#x540E;&#x4E3A;4&#x5B57;&#x8282;&#xFF0C;&#x5173;&#x95ED;&#x6307;&#x9488;&#x538B;&#x7F29;&#xFF08;-XX:
-UseCompressedOops&#xFF09;&#x540E;&#xFF0C;&#x957F;&#x5EA6;&#x4E3A;8&#x5B57;&#x8282;&#xFF0C;&#x5176;&#x6307;&#x5411;&#x7684;&#x4F4D;&#x7F6E;&#x662F;&#x5BF9;&#x8C61;&#x5BF9;&#x5E94;&#x7684;Class&#x5BF9;&#x8C61;&#xFF08;&#x5176;&#x5BF9;&#x5E94;&#x7684;&#x5143;&#x6570;&#x636E;&#x5BF9;&#x8C61;&#xFF09;&#x7684;&#x5185;&#x5B58;&#x5730;&#x5740;</li>
<li>&#x5BF9;&#x8C61;&#x5B9E;&#x4F8B;&#x6570;&#x636E;&#xFF1A;&#x5305;&#x62EC;&#x5BF9;&#x8C61;&#x7684;&#x6240;&#x6709;&#x6210;&#x5458;&#x53D8;&#x91CF;&#xFF0C;&#x5927;&#x5C0F;&#x7531;&#x5404;&#x4E2A;&#x6210;&#x5458;&#x53D8;&#x91CF;&#x51B3;&#x5B9A;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;byte&#x5360;1&#x4E2A;&#x5B57;&#x8282;8&#x6BD4;&#x7279;&#x4F4D;&#x3001;int&#x5360;4&#x4E2A;&#x5B57;&#x8282;32&#x6BD4;&#x7279;&#x4F4D;</li>
<li>&#x5BF9;&#x9F50;&#xFF1A;&#x6700;&#x540E;&#x8FD9;&#x6BB5;&#x7A7A;&#x95F4;&#x8865;&#x5168;&#x5E76;&#x975E;&#x5FC5;&#x987B;&#xFF0C;&#x4EC5;&#x4EC5;&#x4E3A;&#x4E86;&#x7948;&#x7977;&#x5360;&#x4F4D;&#x7B26;&#x7684;&#x4F5C;&#x7528;&#xFF0C;&#x7531;&#x4E8E;HotSpot&#x865A;&#x62DF;&#x673A;&#x7684;&#x5185;&#x5B58;&#x7BA1;&#x7406;&#x7CFB;&#x7EDF;&#x8981;&#x6C42;&#x5BF9;&#x8C61;&#x8D77;&#x59CB;&#x5730;&#x5740;&#x5FC5;&#x987B;&#x662F;8&#x5B57;&#x8282;&#x7684;&#x6574;&#x6570;&#x500D;&#xFF0C;&#x6240;&#x4EE5;&#x5BF9;&#x8C61;&#x5934;&#x6B63;&#x597D;&#x662F;8&#x5B57;&#x8282;&#x7684;&#x500D;&#x6570;&#xFF0C;&#x56E0;&#x6B64;&#x5F53;&#x5BF9;&#x8C61;&#x5B9E;&#x4F8B;&#x6570;&#x636E;&#x90E8;&#x5206;&#x6CA1;&#x6709;&#x5BF9;&#x5176;&#x7684;&#x8BDD;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x5BF9;&#x5176;&#x586B;&#x5145;&#x6765;&#x8865;&#x5168;</li>
</ul>
<p>&#x53E6;&#x5916;&#xFF0C;&#x5728;mark-word&#x9501;&#x7C7B;&#x578B;&#x6807;&#x8BB0;&#x4E2D;&#xFF0C;&#x65E0;&#x9501;&#xFF0C;&#x504F;&#x5411;&#x9501;&#xFF0C;&#x8F7B;&#x91CF;&#x9501;&#xFF0C;&#x91CD;&#x91CF;&#x9501;&#xFF0C;&#x4EE5;&#x53CA;GC&#x6807;&#x8BB0;&#xFF0C;5&#x79CD;&#x7C7B;&#x578B;&#x6CA1;&#x6CD5;&#x7528;2&#x6BD4;&#x7279;&#x6807;&#x8BB0;&#xFF08;2&#x6BD4;&#x7279;&#x6700;&#x7EC8;&#x6709;4&#x79CD;&#x7EC4;&#x5408;00&#x3001;01&#x3001;10&#x3001;11&#xFF09;&#xFF0C;&#x6240;&#x4EE5;&#x65E0;&#x9501;&#x3001;&#x504F;&#x5411;&#x9501;&#xFF0C;&#x524D;&#x53C8;&#x5360;&#x4E86;&#x4EE5;&#x4E3A;&#x504F;&#x5411;&#x9501;&#x6807;&#x8BB0;&#xFF0C;&#x6700;&#x7EC8;&#xFF1A;101&#x4E3A;&#x65E0;&#x9501;&#x3001;001&#x4F4D;&#x504F;&#x5411;&#x9501;&#x3002;</p>
<blockquote>
<p>pom.xml</p>
</blockquote>
<pre class="language-"><code class="lang-xml">
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!--&#x7528;&#x6765;&#x67E5;&#x770B;&#x5BF9;&#x8C61;&#x7ED3;&#x6784; --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.openjdk.jol<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>jol-cli<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>0.14<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>
</code></pre>
<blockquote>
<p>ObjectTest.java</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token constant">VM</span><span class="token punctuation">.</span><span class="token function">current</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">details</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Object</span> obj<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>obj<span class="token operator">+</span><span class="token string">&quot;&#x5341;&#x516D;&#x8FDB;&#x5236;&#x54C8;&#x5E0C;&#xFF1A;&quot;</span><span class="token operator">+</span><span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">toHexString</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">ClassLayout</span><span class="token punctuation">.</span><span class="token function">parseInstance</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toPrintable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<blockquote>
<p>&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;</p>
</blockquote>
<pre class="language-"><code># Running 64-bit HotSpot VM.
# Using compressed oop with 0-bit shift.
# Using compressed klass with 0-bit shift.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

java.lang.Object@6ee52dcd&#x5341;&#x516D;&#x8FDB;&#x5236;&#x54C8;&#x5E0C;&#xFF1A;6ee52dcd
java.lang.Object object internals:
 OFFSET  SIZE    TYPE DESCRIPTION    VALUE
      0     4   (object header)        01 cd 2d e5 (00000001 11001101 00101101 11100101) (-449983231)
      4     4   (object header)        6e 00 00 00 (01101110 00000000 00000000 00000000) (110)
      8     4   (object header)        28 0f a3 16 (00101000 00001111 10100011 00010110) (379785000)
     12     4   (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

Disconnected from the target VM, address: &apos;127.0.0.1:50437&apos;, transport: &apos;socket&apos;

Process finished with exit code 0
</code></pre><ul>
<li>Object&#x5BF9;&#x8C61;&#xFF0C;&#x603B;&#x5171;&#x5360;16&#x5B57;&#x8282;</li>
<li>&#x5BF9;&#x8C61;&#x5934;&#x5360;12&#x4E2A;&#x5B57;&#x8282;&#xFF0C;mark-down&#x5360;8&#x5B57;&#x8282;&#xFF08;01 cd 2d e5 6e 00 00 00&#xFF09;&#x3001;ClassPoint&#x5360;4&#x5B57;&#x8282;&#xFF08;28 0f a3 16&#xFF09;</li>
<li>&#x6700;&#x540E;4&#x4E2A;&#x5B57;&#x8282;&#xFF0C;&#x7528;&#x4E8E;&#x6570;&#x636E;&#x586B;&#x5145;&#x5BF9;&#x9F50;</li>
</ul>
<blockquote>
<p>&#x5173;&#x95ED;&#x6307;&#x9488;&#x538B;&#x7F29;</p>
</blockquote>
<p>&#x5728;Run -&gt; Edit Configuration -&gt; VM Options &#x914D;&#x7F6E;&#x53C2;&#x6570; <code>-XX:-UseCompressedOops</code></p>
<blockquote>
<p>&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;</p>
</blockquote>
<pre class="language-"><code># Running 64-bit HotSpot VM.
# Objects are 8 bytes aligned.
# Field sizes by type: 8, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 8, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

java.lang.Object@6ee52dcd&#x5341;&#x516D;&#x8FDB;&#x5236;&#x54C8;&#x5E0C;&#xFF1A;6ee52dcd
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION    VALUE
      0     4   (object header)        01 cd 2d e5 (00000001 11001101 00101101 11100101) (-449983231)
      4     4   (object header)        6e 00 00 00 (01101110 00000000 00000000 00000000) (110)
      8     4   (object header)        00 1c 85 16 (00000000 00011100 10000101 00010110) (377822208)
     12     4   (object header)        00 00 00 00 (00000000 00000000 00000000 00000000) (0)
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
</code></pre><ul>
<li>&#x5173;&#x95ED;&#x6307;&#x9488;&#x538B;&#x7F29;&#x540E;&#xFF0C;mark-down&#x8FD8;&#x662F;&#x5360;8&#x5B57;&#x8282;&#xFF08;01 cd 2d e5 6e 00 00 00&#xFF09;&#x4E0D;&#x53D8;</li>
<li>&#x91CD;&#x70B9;&#x5728;&#x7C7B;&#x578B;&#x6307;&#x9488;KlassPoint&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x6709;&#x539F;&#x6765;&#x7684;4&#x5B57;&#x8282;&#xFF0C;&#x73B0;&#x5728;&#x6269;&#x5C55;&#x5230;8&#x5B57;&#x8282;&#xFF08;00 1c 85 16 00 00 00 00&#xFF09;</li>
</ul>
<blockquote>
<p>&#x5BF9;&#x8C61;&#x5934;&#x54C8;&#x5E0C;&#x503C;</p>
</blockquote>
<p>&#x6839;&#x636E;&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;&#xFF0C;&#x5BF9;&#x8C61;&#x54C8;&#x5E0C;&#x503C;&#x4E3A;&#x5341;&#x516D;&#x8FDB;&#x5236;&#xFF1A;0x6ee52dcd</p>
<p>&#x5BF9;&#x8C61;&#x5934;&#x4E2D;mark-down&#x4E3A;&#xFF1A;01 cd 2d e5 6e 00 00 00&#xFF0C;&#x7B2C;&#x4E8C;&#x4F4D;&#x5F00;&#x59CB;&#x7684;cd 2d e5 6e&#x521A;&#x597D;&#x662F;&#x5BF9;&#x8C61;&#x54C8;&#x5E0C;&#x503C;&#x7684;&#x5012;&#x5E8F;&#x5B58;&#x50A8;</p>
<p>&#x5173;&#x4E8E;&#x5012;&#x5E8F;&#x5B58;&#x50A8;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x662F;&#x5927;&#x5C0F;&#x7AEF;&#x5B58;&#x50A8;&#x5BFC;&#x81F4;&#x7684;&#xFF1A;</p>
<ul>
<li>Big-Endian&#xFF1A;&#x9AD8;&#x5371;&#x5B57;&#x8282;&#x5B58;&#x653E;&#x4E8E;&#x5185;&#x5B58;&#x7684;&#x4F4E;&#x5730;&#x5740;&#x7AEF;&#xFF0C;&#x5730;&#x4F4D;&#x5B57;&#x8282;&#x5B58;&#x653E;&#x4E8E;&#x5185;&#x5B58;&#x9AD8;&#x5730;&#x5740;&#x7AEF;</li>
<li>Little-Endian&#xFF1A;&#x5730;&#x4F4D;&#x5B57;&#x8282;&#x5B58;&#x653E;&#x4E8E;&#x5185;&#x5B58;&#x7684;&#x4F4E;&#x5730;&#x5740;&#x7AEF;&#xFF0C;&#x9AD8;&#x4F4D;&#x5B57;&#x8282;&#x5B58;&#x653E;&#x4E8E;&#x5185;&#x5B58;&#x7684;&#x9AD8;&#x5730;&#x5740;&#x7AEF;</li>
</ul>
<h2 id="volatile"><a name="volatile" class="anchor-navigation-ex-anchor" href="#volatile"><i class="fa fa-link" aria-hidden="true"></i></a>2.8. volatile</h2>
<p>volatile&#x4FEE;&#x9970;&#x7684;&#x90FD;&#x662F;&#x5C5E;&#x6027;&#x800C;&#x4E0D;&#x662F;&#x65B9;&#x6CD5;&#xFF0C;spring&#x4F7F;&#x7528;volatile&#x6765;&#x4FDD;&#x8BC1;&#x53D8;&#x91CF;&#x5728;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E4B;&#x95F4;&#x7684;&#x53EF;&#x89C1;&#x6027;</p>
<p>&#x51FA;&#x4E8E;&#x8FD0;&#x884C;&#x901F;&#x7387;&#x7684;&#x8003;&#x8651;&#xFF0C;Java&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x628A;&#x7ECF;&#x5E38;&#x8BBF;&#x95EE;&#x7684;&#x53D8;&#x91CF;&#x653E;&#x5230;&#x7F13;&#x5B58;&#x4E2D;&#xFF08;&#x4E25;&#x683C;&#x6765;&#x8BB2;&#x662F;&#x5DE5;&#x4F5C;&#x5185;&#x5B58;&#xFF09;&#x4E2D;&#xFF0C;&#x8BFB;&#x53D6;&#x53D8;&#x91CF;&#x5219;&#x4ECE;&#x7F13;&#x51B2;&#x4E2D;&#x8BFB;&#xFF0C;&#x4F46;&#x662F;&#x591A;&#x7EBF;&#x7A0B;&#x7F16;&#x7A0B;&#x4E2D;&#xFF0C;&#x5185;&#x5B58;&#x4E2D;&#x7684;&#x503C;&#x548C;&#x7F13;&#x5B58;&#x4E2D;&#x7684;&#x503C;&#x53EF;&#x80FD;&#x4F1A;&#x51FA;&#x73B0;&#x4E0D;&#x4E00;&#x81F4;&#xFF0C;volatile&#x7528;&#x4E8E;&#x9650;&#x5B9A;&#x53D8;&#x91CF;&#x53EA;&#x80FD;&#x4ECE;&#x5185;&#x5B58;&#x4E2D;&#x8BFB;&#x53D6;&#xFF0C;&#x4FDD;&#x8BC1;&#x5BF9;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x800C;&#x8A00;&#x503C;&#x662F;&#x4E00;&#x81F4;&#x7684;&#xFF0C;&#x4F46;&#x662F;volatile&#x4E0D;&#x80FD;&#x4FDD;&#x8BC1;&#x539F;&#x5B50;&#x6027;&#xFF0C;&#x4E5F;&#x5C31;&#x4E0D;&#x80FD;&#x4FDD;&#x8BC1;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x3002;</p>
<p>volatile&#x5173;&#x952E;&#x5B57;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x7981;&#x6B62;&#x6307;&#x4EE4;&#x7684;&#x91CD;&#x6392;&#x5E8F;&#x4F18;&#x5316;&#x4EE5;&#x53CA;&#x4EC0;&#x4E48;&#x662F;&#x6307;&#x4EE4;&#x91CD;&#x6392;&#x5E8F;&#x4F18;&#x5316;&#xFF1F;</p>
<h2 id="condition"><a name="condition" class="anchor-navigation-ex-anchor" href="#condition"><i class="fa fa-link" aria-hidden="true"></i></a>2.9. Condition</h2>
<p><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Condition.html" target="_blank">Condition</a>&#x5C06;&#x5BF9;&#x8C61;&#x68C0;&#x6D4B;&#x65B9;&#x6CD5;&#xFF08;wait
notify notifyAll&#xFF09;&#x5206;&#x89E3;&#x6210;&#x4E0D;&#x540C;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x4E0E;Lock&#x5B9E;&#x73B0;&#x914D;&#x5408;&#x4F7F;&#x7528;</p>
<p>Condition&#xFF08;&#x4E5F;&#x6210;&#x4E3A;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x6216;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#xFF09;&#x5728;&#x4E00;&#x5B9A;&#x6761;&#x4EF6;&#x4E0B;&#x91CA;&#x653E;&#x6240;&#x5E76;&#x6302;&#x8D77;&#x7EBF;&#x7A0B;&#xFF0C;&#x5176;&#x672C;&#x8D28;&#x4E0A;&#x7ED1;&#x5B9A;&#x4E00;&#x4E2A;&#x9501;</p>
<p>&#x793A;&#x4F8B;&#xFF1A;&#x6709;&#x754C;&#x7F13;&#x51B2;</p>
<p>&#x7A7A;&#x7F13;&#x51B2;&#x4E0A;&#x6267;&#x884C;take&#x7EBF;&#x7A0B;&#x88AB;&#x963B;&#x585E;&#xFF0C;&#x76F4;&#x5230;&#x7F13;&#x51B2;&#x6DFB;&#x52A0;&#x53EF;&#x7528;&#x5143;&#x7D20;</p>
<p>&#x6EE1;&#x7F13;&#x51B2;&#x4E0A;&#x6267;&#x884C;put&#x7EBF;&#x7A0B;&#x88AB;&#x963B;&#x585E;&#xFF0C;&#x76F4;&#x5230;&#x7F13;&#x51B2;&#x5B58;&#x5728;&#x53EF;&#x7528;&#x7A7A;&#x95F4;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">class</span> <span class="token class-name">BoundedBuffer</span> <span class="token punctuation">{</span>
    <span class="token keyword">final</span> <span class="token class-name">Lock</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReentrantLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">final</span> <span class="token class-name">Condition</span> notFull <span class="token operator">=</span> lock<span class="token punctuation">.</span><span class="token function">newCondition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">final</span> <span class="token class-name">Condition</span> notEmpty <span class="token operator">=</span> lock<span class="token punctuation">.</span><span class="token function">newCondition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">final</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token punctuation">]</span> items <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> putptr<span class="token punctuation">,</span> takeptr<span class="token punctuation">,</span> count<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">put</span><span class="token punctuation">(</span><span class="token class-name">Object</span> x<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
        lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x7F13;&#x51B2;&#x6EE1;&#x4E4B;&#x540E;put&#x64CD;&#x4F5C;&#x963B;&#x585E;</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>count <span class="token operator">==</span> items<span class="token punctuation">.</span>length<span class="token punctuation">)</span>
                notFull<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            items<span class="token punctuation">[</span>putptr<span class="token punctuation">]</span> <span class="token operator">=</span> x<span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">++</span>putptr <span class="token operator">==</span> items<span class="token punctuation">.</span>length<span class="token punctuation">)</span> putptr <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
            <span class="token operator">++</span>count<span class="token punctuation">;</span>
            notEmpty<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">take</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
        lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x7F13;&#x51B2;&#x7A7A;&#x4E4B;&#x540E;take&#x963B;&#x585E;</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>count <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span>
                notEmpty<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">Object</span> x <span class="token operator">=</span> items<span class="token punctuation">[</span>takeptr<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">++</span>takeptr <span class="token operator">==</span> items<span class="token punctuation">.</span>length<span class="token punctuation">)</span> takeptr <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
            <span class="token operator">--</span>count<span class="token punctuation">;</span>
            notFull<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> x<span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>ArrayBlockingQueue &#x5B9E;&#x73B0;&#x4E86;&#x4E0A;&#x8FF0;&#x529F;&#x80FD;</p>
<p>Condition&#x5B9E;&#x73B0;&#x63D0;&#x4F9B;&#x4E86;&#x4E0D;&#x540C;&#x4E8E;&#x5BF9;&#x8C61;&#x68C0;&#x6D4B;&#x65B9;&#x6CD5;&#x7684;&#x884C;&#x4E3A;&#x548C;&#x8BED;&#x4E49;&#xFF0C;&#x6BD4;&#x5982;&#x786E;&#x4FDD;&#x901A;&#x77E5;&#x7684;&#x987A;&#x5E8F;&#xFF0C;&#x6216;&#x8005;&#x5728;&#x6267;&#x884C;&#x901A;&#x77E5;&#x65F6;&#x4E0D;&#x9700;&#x8981;&#x52A0;&#x9501;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Condition</span> <span class="token punctuation">{</span>
    <span class="token keyword">void</span> <span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">awaitUninterruptibly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">long</span> <span class="token function">awaitNanos</span><span class="token punctuation">(</span><span class="token keyword">long</span> nanosTimeout<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span><span class="token punctuation">;</span>

    <span class="token keyword">boolean</span> <span class="token function">await</span><span class="token punctuation">(</span><span class="token keyword">long</span> time<span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span> unit<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span><span class="token punctuation">;</span>

    <span class="token keyword">boolean</span> <span class="token function">awaitUntil</span><span class="token punctuation">(</span><span class="token class-name">Date</span> deadline<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">signalAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="&#x7EBF;&#x7A0B;&#x901A;&#x77E5;&#x7B49;&#x5F85;"><a name="&#x7EBF;&#x7A0B;&#x901A;&#x77E5;&#x7B49;&#x5F85;" class="anchor-navigation-ex-anchor" href="#&#x7EBF;&#x7A0B;&#x901A;&#x77E5;&#x7B49;&#x5F85;"><i class="fa fa-link" aria-hidden="true"></i></a>2.10. &#x7EBF;&#x7A0B;&#x901A;&#x77E5;&#x7B49;&#x5F85;</h2>
<blockquote>
<p>&#x4E24;&#x4E2A;&#x7EBF;&#x7A0B;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token keyword">class</span> <span class="token class-name">MyList</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">List</span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;&#x5143;&#x7D20;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">ThreadA</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">MyList</span> list<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">ThreadA</span><span class="token punctuation">(</span><span class="token class-name">MyList</span> list<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>list <span class="token operator">=</span> list<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6DFB;&#x52A0;&quot;</span> <span class="token operator">+</span> <span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;&#x4E2A;&#x5143;&#x7D20;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">ThreadB</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">MyList</span> list<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">ThreadB</span><span class="token punctuation">(</span><span class="token class-name">MyList</span> list<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>list <span class="token operator">=</span> list<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x65E0;&#x7EBF;&#x5FAA;&#x73AF;&#x5F88;&#x6D88;&#x8017;&#x6027;&#x80FD;</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// &#x6D4B;&#x8BD5;&#x8FD9;&#x91CC;&#x6CA1;&#x6709;&#x6B63;&#x5E38;&#x8F93;&#x51FA;&#xFF0C;&#x4F7F;&#x7528;sleep&#x624D;&#x6B63;&#x5E38;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;==5,&#x9000;&#x51FA;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">InterruptedException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                <span class="token comment">// Thread.sleep(1);</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">MyList</span> myList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MyList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">ThreadA</span> threadA <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadA</span><span class="token punctuation">(</span>myList<span class="token punctuation">)</span><span class="token punctuation">;</span>
        threadA<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        threadA<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">ThreadB</span> threadB <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadB</span><span class="token punctuation">(</span>myList<span class="token punctuation">)</span><span class="token punctuation">;</span>
        threadB<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">&quot;b&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        threadB<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>Java&#x4E2D;&#x5BF9;&#x5E94;&#x7B49;&#x5F85;/&#x901A;&#x77E5;&#x7684;&#x65B9;&#x6CD5;&#x662F;wait/notify&#xFF0C;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x90FD;&#x662F;&#x8D85;&#x7C7B;Object&#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4EFB;&#x4F55;&#x5BF9;&#x8C61;&#x90FD;&#x53EF;&#x4EE5;&#x52A0;&#x9501;&#xFF0C;&#x800C;wait/notify&#x662F;&#x7531;&#x9501;&#x8C03;&#x7528;&#x7684;</p>
<blockquote>
<p>wait&#x65B9;&#x6CD5;</p>
</blockquote>
<p>&#xFF08;1&#xFF09;wait()&#x7684;&#x4F5C;&#x7528;&#x662F;&#x4F7F;&#x5F53;&#x524D;&#x6267;&#x884C;&#x4EE3;&#x7801;&#x7684;&#x7EBF;&#x7A0B;&#x8FDB;&#x884C;&#x7B49;&#x5F85;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x4F1A;&#x5C06;&#x8BE5;&#x7EBF;&#x7A0B;&#x653E;&#x5165;&#x201C;&#x9884;&#x6267;&#x884C;&#x961F;&#x5217;&#x201D;&#x4E2D;&#xFF0C;&#x5E76;&#x4E14;&#x5728;wait()&#x6240;&#x5728;&#x7684;&#x4EE3;&#x7801;&#x5904;&#x505C;&#x6B62;&#x6267;&#x884C;&#xFF0C;&#x76F4;&#x5230;&#x63A5;&#x5230;&#x901A;&#x77E5;&#x6216;&#x88AB;&#x4E2D;&#x65AD;&#x4E3A;&#x6B62;</p>
<p>&#xFF08;2&#xFF09;&#x5728;&#x8C03;&#x7528;wait()&#x4E4B;&#x524D;&#xFF0C;&#x7EBF;&#x7A0B;&#x5FC5;&#x987B;&#x83B7;&#x5F97;&#x8BE5;&#x5BF9;&#x8C61;&#x7EA7;&#x522B;&#x9501;&#xFF0C;&#x5373;&#x53EA;&#x80FD;&#x5728;&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#x6216;&#x540C;&#x6B65;&#x5757;&#x4E2D;&#x8C03;&#x7528;wait()&#x65B9;&#x6CD5;</p>
<p>&#xFF08;3&#xFF09;wait()&#x662F;&#x91CA;&#x653E;&#x9501;&#x7684;&#xFF0C;&#x5373;&#x5728;&#x6267;&#x884C;&#x5230;wait()&#x65B9;&#x6CD5;&#x4E4B;&#x540E;&#xFF0C;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4F1A;&#x91CA;&#x653E;&#x9501;&#xFF0C;&#x5F53;&#x4ECE;wait()&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x524D;&#xFF0C;&#x7EBF;&#x7A0B;&#x4E0E;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x7ADE;&#x4E89;&#x91CD;&#x65B0;&#x83B7;&#x5F97;&#x9501;</p>
<blockquote>
<p>notify&#x65B9;&#x6CD5;</p>
</blockquote>
<p>&#xFF08;1&#xFF09;notify()&#x65B9;&#x6CD5;&#x4E5F;&#x8981;&#x5728;&#x540C;&#x6B65;&#x5757;&#x6216;&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#x4E2D;&#x8C03;&#x7528;&#xFF0C;&#x5373;&#x5728;&#x8C03;&#x7528;&#x524D;&#xFF0C;&#x7EBF;&#x7A0B;&#x4E5F;&#x5FC5;&#x987B;&#x83B7;&#x5F97;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x5BF9;&#x8C61;&#x7EA7;&#x522B;&#x9501;</p>
<p>&#xFF08;2&#xFF09;&#x8BE5;&#x65B9;&#x6CD5;&#x662F;&#x7528;&#x6765;&#x901A;&#x77E5;&#x90A3;&#x4E9B;&#x53EF;&#x80FD;&#x7B49;&#x5F85;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x5BF9;&#x8C61;&#x9501;&#x7684;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x7B49;&#x5F85;&#xFF0C;&#x5219;&#x6709;&#x7EBF;&#x7A0B;&#x89C4;&#x5212;&#x5668;&#x968F;&#x673A;&#x6311;&#x9009;&#x51FA;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x5448;wait&#x72B6;&#x6001;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x5BF9;&#x9F50;&#x5176;&#x53D1;&#x51FA;&#x901A;&#x77E5;notify&#xFF0C;&#x5E76;&#x4F7F;&#x5B83;&#x7B49;&#x5F85;&#x83B7;&#x53D6;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x5BF9;&#x8C61;&#x9501;</p>
<p>&#xFF08;3&#xFF09;&#x6267;&#x884C;notify&#x65B9;&#x6CD5;&#x4E4B;&#x540E;&#xFF0C;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4E0D;&#x4F1A;&#x7ACB;&#x5373;&#x91CA;&#x653E;&#x5176;&#x62E5;&#x6709;&#x7684;&#x8BE5;&#x5BF9;&#x8C61;&#x9501;&#xFF0C;&#x800C;&#x662F;&#x6267;&#x884C;&#x5B8C;&#x4E4B;&#x540E;&#x624D;&#x4F1A;&#x91CA;&#x653E;&#x8BE5;&#x5BF9;&#x8C61;&#x9501;&#xFF0C;&#x88AB;&#x901A;&#x77E5;&#x7684;&#x7EBF;&#x7A0B;&#x4E5F;&#x4E0D;&#x4F1A;&#x7ACB;&#x5373;&#x83B7;&#x5F97;&#x5BF9;&#x8C61;&#x9501;&#xFF0C;&#x800C;&#x662F;&#x7B49;&#x5F85;notify&#x65B9;&#x6CD5;&#x6267;&#x884C;&#x5B8C;&#x4E4B;&#x540E;&#xFF0C;&#x91CA;&#x653E;&#x4E86;&#x8BE5;&#x5BF9;&#x8C61;&#x9501;&#xFF0C;&#x624D;&#x53EF;&#x4EE5;&#x83B7;&#x5F97;&#x8BE5;&#x5BF9;&#x8C61;&#x9501;</p>
<p>&#xFF08;4&#xFF09;notifyAll()&#x901A;&#x77E5;&#x6240;&#x6709;&#x7B49;&#x5F85;&#x540C;&#x4E00;&#x5171;&#x4EAB;&#x8D44;&#x6E90;&#x7684;&#x5168;&#x90E8;&#x7EBF;&#x7A0B;&#x4ECE;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x9000;&#x51FA;&#xFF0C;&#x8FDB;&#x5165;&#x53EF;&#x8FD0;&#x884C;&#x72B6;&#x6001;&#xFF0C;&#x91CD;&#x65B0;&#x7ADE;&#x4E89;&#x83B7;&#x5F97;&#x5BF9;&#x8C61;&#x9501;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">class</span> <span class="token class-name">MyList</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">List</span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;&#x5143;&#x7D20;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">ThreadA</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">Object</span> lock<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">ThreadA</span><span class="token punctuation">(</span><span class="token class-name">Object</span> lock<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>lock <span class="token operator">=</span> lock<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lock<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">MyList</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;wait begin &quot;</span> <span class="token operator">+</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    lock<span class="token punctuation">.</span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;wait end &quot;</span> <span class="token operator">+</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">ThreadB</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">Object</span> lock<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">ThreadB</span><span class="token punctuation">(</span><span class="token class-name">Object</span> lock<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>lock <span class="token operator">=</span> lock<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lock<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token class-name">MyList</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">MyList</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token comment">// notify&#x53D1;&#x51FA;&#x901A;&#x77E5;&#x540E;&#x4E0D;&#x91CA;&#x653E;&#x5BF9;&#x8C61;&#x9501;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF0C;&#x6267;&#x884C;&#x5B8C;&#x4E4B;&#x540E;wait&#x624D;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x9501;</span>
                        lock<span class="token punctuation">.</span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x5DF2;&#x53D1;&#x51FA;&#x901A;&#x77E5;&#xFF01;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6DFB;&#x52A0;&#x4E86;&quot;</span> <span class="token operator">+</span> <span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;&#x4E2A;&#x5143;&#x7D20;&#xFF01;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token class-name">Object</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">ThreadA</span> threadA <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadA</span><span class="token punctuation">(</span>lock<span class="token punctuation">)</span><span class="token punctuation">;</span>
            threadA<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">ThreadB</span> threadB <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadB</span><span class="token punctuation">(</span>lock<span class="token punctuation">)</span><span class="token punctuation">;</span>
            threadB<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>wait/notify&#x603B;&#x7ED3;</p>
</blockquote>
<p>&#xFF08;1&#xFF09;wait/notify&#x8981;&#x7ED3;&#x5408;synchronized&#x5173;&#x952E;&#x5B57;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#xFF0C;&#x56E0;&#x4E3A;&#x4ED6;&#x4EEC;&#x90FD;&#x9700;&#x8981;&#x9996;&#x5148;&#x83B7;&#x53D6;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x5BF9;&#x8C61;&#x9501;</p>
<p>&#xFF08;2&#xFF09;wait&#x65B9;&#x6CD5;&#x662F;&#x91CA;&#x653E;&#x9501;&#xFF0C;notify&#x65B9;&#x6CD5;&#x662F;&#x4E0D;&#x91CA;&#x653E;&#x9501;&#x7684;</p>
<blockquote>
<p>&#x963B;&#x585E;&#x961F;&#x5217;&#x6848;&#x4F8B;</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token keyword">class</span> <span class="token class-name">MyQueue</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x96C6;&#x5408;&#x5BB9;&#x5668;</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">LinkedList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x8BA1;&#x6570;&#x5668;</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">AtomicInteger</span> count <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AtomicInteger</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x5BB9;&#x5668;&#x4E0A;&#x4E0B;&#x9650;</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">int</span> maxSize <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">int</span> minSize <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x5BF9;&#x8C61;&#x9501;</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">Object</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">put</span><span class="token punctuation">(</span><span class="token class-name">Object</span> obj<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lock<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;put&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// &#x5BB9;&#x5668;&#x6EE1;&#x540E;put&#x7B49;&#x5F85;</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>count<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> maxSize<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    lock<span class="token punctuation">.</span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
            count<span class="token punctuation">.</span><span class="token function">getAndIncrement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x751F;&#x4EA7;&#x5143;&#x7D20;&#xFF1A;&quot;</span> <span class="token operator">+</span> obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
            lock<span class="token punctuation">.</span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Object</span> temp<span class="token punctuation">;</span>
        <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lock<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;get&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// &#x5BB9;&#x5668;&#x7A7A;&#x540E;get&#x7B49;&#x5F85;</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>count<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> minSize<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    lock<span class="token punctuation">.</span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            count<span class="token punctuation">.</span><span class="token function">getAndDecrement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            temp <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">removeFirst</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6D88;&#x8D39;&#x5143;&#x7D20;&#xFF1A;&quot;</span> <span class="token operator">+</span> temp<span class="token punctuation">)</span><span class="token punctuation">;</span>
            lock<span class="token punctuation">.</span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> temp<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">int</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> count<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">initMyQueue</span><span class="token punctuation">(</span><span class="token class-name">MyQueue</span> myQueue<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        myQueue<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        myQueue<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;b&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        myQueue<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;c&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        myQueue<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;d&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        myQueue<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;e&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x5F53;&#x524D;&#x5143;&#x7D20;&#x4E2A;&#x6570;&#xFF1A;&quot;</span> <span class="token operator">+</span> myQueue<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
        <span class="token class-name">MyQueue</span> myQueue <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MyQueue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">initMyQueue</span><span class="token punctuation">(</span>myQueue<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">Thread</span> t1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
            myQueue<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;h&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            myQueue<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;i&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">&quot;t1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">Thread</span> t2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                myQueue<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                myQueue<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">&quot;t2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        t1<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        t2<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">/*
&#x6267;&#x884C;&#x6B65;&#x9AA4;&#xFF1A;
1.initMyQueue
2.t1.put(&quot;h&quot;) -&gt; wait
3.t2.get() -&gt; notify
4.t1.put(&quot;h&quot;) -&gt; do
5.t1.put(&quot;i&quot;) -&gt; wait
6.t2.get() -&gt; notify
7.t1.put(&quot;i&quot;) -&gt; do
*/</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#x4E8B;&#x9879;</p>
</blockquote>
<ol>
<li>wait&#x548C;notify&#x65B9;&#x6CD5;&#x8981;&#x5728;&#x540C;&#x6B65;&#x5757;&#x6216;&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#x4E2D;&#x8C03;&#x7528;&#xFF0C;&#x5373;&#x5728;&#x8C03;&#x7528;&#x524D;&#xFF0C;&#x7EBF;&#x7A0B;&#x4E5F;&#x5FC5;&#x987B;&#x83B7;&#x5F97;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x5BF9;&#x8C61;&#x7EA7;&#x522B;&#x9501;</li>
<li>wait&#x65B9;&#x6CD5;&#x662F;&#x91CA;&#x653E;&#x9501;&#xFF0C;notify&#x65B9;&#x6CD5;&#x662F;&#x4E0D;&#x91CA;&#x653E;&#x9501;&#x7684;</li>
<li>notify&#x6BCF;&#x6B21;&#x5524;&#x9192;wait&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x7684;&#x7EBF;&#x7A0B;&#x90FD;&#x662F;&#x968F;&#x673A;&#x7684;&#xFF0C;&#x4E14;&#x6BCF;&#x6B21;&#x53EA;&#x5524;&#x9192;&#x4E00;&#x4E2A;</li>
<li>notifyAll&#x6BCF;&#x6B21;&#x5524;&#x9192;wait&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x7684;&#x7EBF;&#x7A0B;&#x4F7F;&#x4E4B;&#x91CD;&#x65B0;&#x7ADE;&#x4E89;&#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x9501;&#xFF0C;&#x4F18;&#x5148;&#x7EA7;&#x6700;&#x9AD8;&#x7684;&#x90A3;&#x4E2A;&#x7EBF;&#x7A0B;&#x4F1A;&#x6700;&#x5148;&#x6267;&#x884C;</li>
<li>&#x5F53;&#x7EBF;&#x7A0B;&#x5904;&#x4E8E;wait&#x72B6;&#x6001;&#x65F6;&#xFF0C;&#x8C03;&#x7528;&#x7EBF;&#x7A0B;&#x5BF9;&#x8C61;&#x7684;interrupt()&#x65B9;&#x6CD5;&#x4F1A;&#x51FA;&#x73B0;InterruotedException&#x5F02;&#x5E38;</li>
</ol>
<blockquote>
<p>&#x5176;&#x4ED6;&#x77E5;&#x8BC6;&#x70B9;</p>
</blockquote>
<p>&#xFF08;1&#xFF09;&#x8FDB;&#x7A0B;&#x95F4;&#x7684;&#x901A;&#x4FE1;&#x65B9;&#x5F0F;&#xFF1A;</p>
<p>&#x7BA1;&#x9053;Pipe &#x6709;&#x540D;&#x7BA1;&#x9053;named pipe &#x4FE1;&#x53F7;&#x91CF;semophore &#x6D88;&#x606F;&#x961F;&#x5217;message queue &#x4FE1;&#x53F7;signal</p>
<p>&#x5171;&#x4EAB;&#x5185;&#x5B58;shared memory &#x5957;&#x63A5;&#x5B57;socket</p>
<p>&#xFF08;2&#xFF09;&#x7EBF;&#x7A0B;&#x95F4;&#x7684;&#x901A;&#x4FE1;&#x65B9;&#x5F0F;</p>
<ul>
<li><p>&#x9501;&#x673A;&#x5236;&#xFF1A;</p>
</li>
<li><p>&#x4E92;&#x65A5;&#x9501;&#xFF1A;&#x63D0;&#x4F9B;&#x4E86;&#x4EE5;&#x6392;&#x5B83;&#x65B9;&#x5F0F;&#x963B;&#x6B62;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x88AB;&#x5E76;&#x53D1;&#x4FEE;&#x6539;&#x7684;&#x65B9;&#x6CD5;</p>
</li>
<li>&#x8BFB;&#x5199;&#x9501;&#xFF1A;&#x5141;&#x8BB8;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x8BFB;&#x53D6;&#x5171;&#x4EAB;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x5BF9;&#x5199;&#x64CD;&#x4F5C;&#x4E92;&#x65A5;</li>
<li><p>&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#xFF1A;&#x53EF;&#x4EE5;&#x4EE5;&#x539F;&#x5B50;&#x7684;&#x65B9;&#x5F0F;&#x963B;&#x585E;&#x8FDB;&#x7A0B;&#xFF0C;&#x76F4;&#x5230;&#x67D0;&#x4E2A;&#x7279;&#x5B9A;&#x6761;&#x4EF6;&#x4E3A;&#x771F;&#x4E3A;&#x6B62;</p>
</li>
<li><p>&#x4FE1;&#x53F7;&#x91CF;&#x673A;&#x5236;</p>
</li>
</ul>
<p>&#x5305;&#x62EC;&#x65E0;&#x540D;&#x7EBF;&#x7A0B;&#x4FE1;&#x53F7;&#x91CF;&#x4E0E;&#x6709;&#x540D;&#x7EBF;&#x7A0B;&#x4FE1;&#x53F7;&#x91CF;</p>
<ul>
<li>&#x4FE1;&#x53F7;&#x673A;&#x5236;&#xFF1A;</li>
</ul>
<p>&#x7C7B;&#x4F3C;&#x4E8E;&#x8FDB;&#x7A0B;&#x95F4;&#x7684;&#x4FE1;&#x53F7;&#x5904;&#x7406;</p>
<p>&#x7EBF;&#x7A0B;&#x95F4;&#x901A;&#x4FE1;&#x7684;&#x4E3B;&#x8981;&#x76EE;&#x7684;&#x662F;&#x7528;&#x4E8E;&#x7EBF;&#x7A0B;&#x540C;&#x6B65;&#xFF0C;&#x6240;&#x4EE5;&#x7EBF;&#x7A0B;&#x6CA1;&#x6709;&#x50CF;&#x8FDB;&#x7A0B;&#x901A;&#x4FE1;&#x4E2D;&#x7528;&#x4E8E;&#x6570;&#x636E;&#x4EA4;&#x6362;&#x7684;&#x901A;&#x4FE1;&#x673A;&#x5236;</p>
<h2 id="lock"><a name="lock" class="anchor-navigation-ex-anchor" href="#lock"><i class="fa fa-link" aria-hidden="true"></i></a>2.11. Lock</h2>
<p>lock&#x6BD4;synchronized&#x5177;&#x6709;&#x66F4;&#x591A;&#x7684;&#x64CD;&#x4F5C;&#x5C5E;&#x6027;&#xFF0C;lock&#x65F6;&#x4E00;&#x79CD;&#x63A7;&#x5236;&#x591A;&#x7EBF;&#x7A0B;&#x5BF9;&#x5171;&#x4EAB;&#x8D44;&#x6E90;&#x8BBF;&#x95EE;&#x7684;&#x5DE5;&#x5177;</p>
<p>&#x9664;ReadWriteLock&#x53EF;&#x8FD0;&#x884C;&#x5E76;&#x53D1;&#x8BBF;&#x95EE;&#x5171;&#x4EAB;&#x8D44;&#x6E90;&#x5916;&#xFF0C;&#x4E00;&#x822C;&#x90FD;&#x662F;&#x4E00;&#x6B21;&#x53EA;&#x5141;&#x8BB8;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x83B7;&#x5F97;&#x5171;&#x4EAB;&#x8D44;&#x6E90;&#x7684;&#x8BBF;&#x95EE;&#x9501;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Lock</span> <span class="token punctuation">{</span>
    <span class="token keyword">void</span> <span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">lockInterruptibly</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span><span class="token punctuation">;</span>

    <span class="token keyword">boolean</span> <span class="token function">tryLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">boolean</span> <span class="token function">tryLock</span><span class="token punctuation">(</span><span class="token keyword">long</span> time<span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span> unit<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">Condition</span> <span class="token function">newCondition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="reentrantlock"><a name="reentrantlock" class="anchor-navigation-ex-anchor" href="#reentrantlock"><i class="fa fa-link" aria-hidden="true"></i></a>2.12. ReentrantLock</h2>
<p>&#x4E00;&#x4E2A;&#x53EF;&#x91CD;&#x5165;&#x7684;&#x4E92;&#x65A5;&#x9501;&#xFF0C;&#x57FA;&#x672C;&#x884C;&#x4E3A;&#x548C;&#x8BED;&#x4E49;&#x4E0E;&#x4F7F;&#x7528;synchronized&#x65B9;&#x6CD5;&#x548C;&#x8BED;&#x53E5;&#x8BBF;&#x95EE;&#x7684;&#x9690;&#x5F0F;&#x76D1;&#x63A7;&#x9501;&#x76F8;&#x540C;&#xFF0C;&#x4F46;&#x5177;&#x6709;&#x6269;&#x5C55;&#x529F;&#x80FD;</p>
<blockquote>
<p>ReentrantLock&#x540C;&#x6B65;&#x6848;&#x4F8B;</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">runMethod</span><span class="token punctuation">(</span><span class="token class-name">Lock</span> lock<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;ThreadName:&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token string">&quot; i=&quot;</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Lock</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReentrantLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// lambda&#x5199;&#x6CD5;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">runMethod</span><span class="token punctuation">(</span>lock<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;thread1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">runMethod</span><span class="token punctuation">(</span>lock<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;thread2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">runMethod</span><span class="token punctuation">(</span>lock<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;thread3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">runMethod</span><span class="token punctuation">(</span>lock<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;thread4&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x5E38;&#x89C4;&#x5199;&#x6CD5;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">runMethod</span><span class="token punctuation">(</span>lock<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">&quot;thread5&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>reentrantLock&#x91CA;&#x653E;&#x9501;&#x4E4B;&#x540E;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x624D;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x9501;&#xFF0C;&#x7EBF;&#x7A0B;&#x4E4B;&#x95F4;&#x6267;&#x884C;&#x7684;&#x987A;&#x5E8F;&#x662F;&#x968F;&#x673A;&#x7684;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">runMethod</span><span class="token punctuation">(</span><span class="token class-name">Lock</span> lock<span class="token punctuation">,</span> <span class="token keyword">long</span> sleepTime<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span>sleepTime<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;ThreadName:&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Lock</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReentrantLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">runMethod</span><span class="token punctuation">(</span>lock<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;thread1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">runMethod</span><span class="token punctuation">(</span>lock<span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;thread2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">runMethod</span><span class="token punctuation">(</span>lock<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;thread3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">runMethod</span><span class="token punctuation">(</span>lock<span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;thread4&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">runMethod</span><span class="token punctuation">(</span>lock<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;thread5&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x6848;&#x4F8B;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;&#x5728;sleep&#x6307;&#x5B9A;&#x7684;&#x65F6;&#x95F4;&#x5185;&#xFF0C;&#x5F53;&#x8C03;&#x7528;&#x4E86;lock.lock()&#x65B9;&#x6CD5;&#x7EBF;&#x7A0B;&#x5C31;&#x6301;&#x6709;&#x4E86;&#x201C;&#x5BF9;&#x8C61;&#x76D1;&#x89C6;&#x5668;&#x201D;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x53EA;&#x80FD;&#x7B49;&#x5F85;&#x9501;&#x88AB;&#x91CA;&#x653E;&#x540E;&#x518D;&#x6B21;&#x7ADE;&#x4E89;&#xFF0C;&#x6548;&#x679C;&#x548C;&#x4F7F;&#x7528;synchronized&#x5173;&#x952E;&#x5B57;&#x662F;&#x4E00;&#x6837;&#x7684;</p>
<blockquote>
<p>ReentrantLock&#x7EBF;&#x7A0B;&#x901A;&#x4FE1;</p>
</blockquote>
<p>synchronized&#x4F7F;&#x7528;wait()&#x65B9;&#x6CD5;&#x548C;notify()&#x65B9;&#x6CD5;&#x7ED3;&#x5408;&#x5B9E;&#x73B0;&#x7EBF;&#x7A0B;&#x95F4;&#x901A;&#x4FE1;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x7B49;&#x5F85;/&#x901A;&#x77E5;&#x6A21;&#x5F0F;</p>
<p>ReentrantLock&#x4E2D;&#x4F7F;&#x7528;Condition&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5B9E;&#x73B0;&#x7EBF;&#x7A0B;&#x95F4;&#x901A;&#x4FE1;</p>
<ul>
<li>Object&#x7684;wait()&#x65B9;&#x6CD5;&#x76F8;&#x5F53;&#x4E8E;Condition&#x7C7B;&#x4E2D;&#x7684;await()&#x65B9;&#x6CD5;</li>
<li>Object&#x7684;notify()&#x65B9;&#x6CD5;&#x76F8;&#x5F53;&#x4E8E;Condition&#x7C7B;&#x4E2D;&#x7684;signal()&#x65B9;&#x6CD5;</li>
<li>Object&#x7684;notifyAll()&#x65B9;&#x6CD5;&#x76F8;&#x5F53;&#x4E8E;Condition&#x7C7B;&#x4E2D;&#x7684;SignalAll()&#x65B9;&#x6CD5;</li>
</ul>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">Lock</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReentrantLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Condition</span> condition <span class="token operator">=</span> lock<span class="token punctuation">.</span><span class="token function">newCondition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">testAwait</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x5F00;&#x59CB;await ThreadName&#xFF1A;&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            condition<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;await&#x7ED3;&#x675F; ThreadName&#xFF1A;&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">testSignal</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x53D1;&#x9001;&#x901A;&#x77E5;signal ThreadName&#xFF1A;&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        condition<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> test<span class="token punctuation">.</span><span class="token function">testAwait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;t1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">3000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> test<span class="token punctuation">.</span><span class="token function">testSignal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;t2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">/*
&#x6267;&#x884C;&#x6B65;&#x9AA4;&#xFF1A;
1.t1.testAwait() -&gt; await
2.t2.testSignal() -&gt; signal
3.t1.testAwait() -&gt; do
*/</span>
</code></pre>
<p>Lock&#x5BF9;&#x8C61;&#x548C;&#x591A;&#x4E2A;Condition&#x5B9E;&#x73B0;&#x7B49;&#x5F85;/&#x901A;&#x77E5;&#x5B9E;&#x4F8B;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">Lock</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReentrantLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Condition</span> conditionA <span class="token operator">=</span> lock<span class="token punctuation">.</span><span class="token function">newCondition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Condition</span> conditionB <span class="token operator">=</span> lock<span class="token punctuation">.</span><span class="token function">newCondition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">testAwait</span><span class="token punctuation">(</span><span class="token class-name">Condition</span> condition<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x5F00;&#x59CB;await ThreadName&#xFF1A;&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            condition<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;await&#x7ED3;&#x675F; ThreadName&#xFF1A;&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">testSignal</span><span class="token punctuation">(</span><span class="token class-name">Condition</span> condition<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x53D1;&#x9001;&#x901A;&#x77E5;signal ThreadName&#xFF1A;&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        condition<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> test<span class="token punctuation">.</span><span class="token function">testAwait</span><span class="token punctuation">(</span>test<span class="token punctuation">.</span>conditionA<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;t1_conditionA&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> test<span class="token punctuation">.</span><span class="token function">testAwait</span><span class="token punctuation">(</span>test<span class="token punctuation">.</span>conditionB<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;t2_conditionB&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> test<span class="token punctuation">.</span><span class="token function">testSignal</span><span class="token punctuation">(</span>test<span class="token punctuation">.</span>conditionA<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;t3_conditionA&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7A0D;&#x7B49;5&#x79D2;&#x518D;&#x901A;&#x77E5;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#xFF01;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> test<span class="token punctuation">.</span><span class="token function">testSignal</span><span class="token punctuation">(</span>test<span class="token punctuation">.</span>conditionB<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;t4_conditionB&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="reentrantreadwritelock"><a name="reentrantreadwritelock" class="anchor-navigation-ex-anchor" href="#reentrantreadwritelock"><i class="fa fa-link" aria-hidden="true"></i></a>2.13. ReentrantReadWriteLock</h2>
<p>&#x96C6;&#x5408;&#x4E2D;&#x6709;&#x540C;&#x6B65;&#x5BB9;&#x5668;HashTable&#xFF08;&#x8BFB;&#x5199;&#x90FD;&#x4E92;&#x65A5;&#xFF09;&#x548C;&#x5E76;&#x53D1;&#x5BB9;&#x5668;ConcurrentHashMap</p>
<p>ReentrantLock&#x662F;&#x5B8C;&#x5168;&#x4E92;&#x65A5;&#x7684;&#xFF0C;&#x5373;&#x540C;&#x4E00;&#x65F6;&#x95F4;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6267;&#x884C;lock()&#x4E4B;&#x540E;&#x7684;&#x4EFB;&#x52A1;</p>
<p>&#x4E3A;&#x63D0;&#x9AD8;&#x6548;&#x7387;&#xFF0C;&#x589E;&#x52A0;ReentrantReadWriteLock&#x9501;&#xFF1A;&#x8BFB;&#x9501;&#x4E3A;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x5199;&#x9501;&#x4E3A;&#x4E92;&#x65A5;&#x9501;&#xFF0C;&#x8BFB;&#x9501;&#x548C;&#x5199;&#x9501;&#x4E92;&#x65A5;</p>
<blockquote>
<p>&#x6D4B;&#x8BD5;&#x8BFB;&#x9501;&#x5171;&#x4EAB;</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">ReentrantReadWriteLock</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReentrantReadWriteLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">readLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x83B7;&#x5F97;&#x8BFB;&#x9501;&#xFF1A;&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                    <span class="token operator">+</span> <span class="token string">&quot; &#x65F6;&#x95F4;&#xFF1A;&quot;</span> <span class="token operator">+</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">readLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> test<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;t1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> test<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;t2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x6D4B;&#x8BD5;&#x5199;&#x9501;&#x4E92;&#x65A5;</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">ReentrantReadWriteLock</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReentrantReadWriteLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">write</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">writeLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x83B7;&#x5F97;&#x8BFB;&#x9501;&#xFF1A;&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                    <span class="token operator">+</span> <span class="token string">&quot; &#x65F6;&#x95F4;&#xFF1A;&quot;</span> <span class="token operator">+</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            lock<span class="token punctuation">.</span><span class="token function">writeLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> test<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;t1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> test<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;t2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x6D4B;&#x8BD5;&#x8BFB;&#x5199;&#x9501;&#x4E92;&#x65A5;</p>
</blockquote>
<pre class="language-"><code>public class Test {
    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    private void read() {
        try {
            lock.readLock().lock();
            System.out.println(&quot;&#x83B7;&#x5F97;&#x8BFB;&#x9501;&#xFF1A;&quot; + Thread.currentThread().getName() 
            + &quot; &#x65F6;&#x95F4;&#xFF1A;&quot; + System.currentTimeMillis());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.readLock().unlock();
        }
    }

    private void write() {
        try {
            lock.writeLock().lock();
            System.out.println(&quot;&#x83B7;&#x5F97;&#x8BFB;&#x9501;&#xFF1A;&quot; + Thread.currentThread().getName() 
            + &quot; &#x65F6;&#x95F4;&#xFF1A;&quot; + System.currentTimeMillis());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.writeLock().unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Test test = new Test();
        // new Thread(() -&gt; test.read(), &quot;t1&quot;).start();
        new Thread(test::read, &quot;t1&quot;).start();
        Thread.sleep(1000);
        // new Thread(() -&gt; test.write(), &quot;t2&quot;).start();
        new Thread(test::write, &quot;t2&quot;).start();
    }
}
</code></pre><h2 id="countdownlatch"><a name="countdownlatch" class="anchor-navigation-ex-anchor" href="#countdownlatch"><i class="fa fa-link" aria-hidden="true"></i></a>2.14. CountDownLatch</h2>
<p>JDK1.5&#x6DFB;&#x52A0;&#x4E86;CountDownLatch&#xFF0C;&#x5B58;&#x5728;&#x4E8E;java.util.concurrent&#x5305;&#x4E0B;</p>
<p>CountDownLatch&#x7C7B;&#x53EF;&#x4EE5;&#x4F7F;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x7B49;&#x5F85;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x5404;&#x81EA;&#x7684;&#x5DE5;&#x4F5C;&#x540E;&#x518D;&#x6267;&#x884C;</p>
<p>CountDownLatch&#x662F;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x5B9E;&#x7528;&#x7684;&#x591A;&#x7EBF;&#x7A0B;&#x63A7;&#x5236;&#x5DE5;&#x5177;&#x7C7B;&#xFF0C;&#x6210;&#x4E3A;&#x201C;&#x5012;&#x8BA1;&#x65F6;&#x5668;&#x201D;</p>
<p>CountDownLatch&#x662F;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x8BA1;&#x6570;&#x5668;&#x7C7B;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x521D;&#x59CB;&#x503C;&#x4E3A;&#x7EBF;&#x7A0B;&#x7684;&#x6570;&#x91CF;</p>
<p>&#x6BCF;&#x5F53;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x81EA;&#x5DF1;&#x7684;&#x4EFB;&#x52A1;&#x540E;&#xFF0C;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x503C;&#x5C31;&#x4F1A;&#x51CF;1</p>
<p>&#x5F53;&#x8BA1;&#x6570;&#x5668;&#x4E3A;0&#x65F6;&#xFF0C;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x5DF2;&#x5B8C;&#x6210;&#xFF0C;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x6062;&#x590D;&#x6267;&#x884C;&#x4EFB;&#x52A1;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">int</span> <span class="token constant">COUNT</span> <span class="token operator">=</span> <span class="token number">7</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">CountDownLatch</span> latch <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CountDownLatch</span><span class="token punctuation">(</span><span class="token constant">COUNT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token constant">COUNT</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6536;&#x96C6;&#x7B2C;&quot;</span> <span class="token operator">+</span> index <span class="token operator">+</span> <span class="token string">&quot;&#x9897;&#x9F99;&#x73E0;&#xFF01;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token number">3000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                <span class="token comment">// &#x6536;&#x96C6;&#x5230;&#x4E00;&#x9897;&#x9F99;&#x73E0;&#xFF0C;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x6536;&#x96C6;&#x7684;&#x6570;&#x91CF;&#x51CF;1</span>
                latch<span class="token punctuation">.</span><span class="token function">countDown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// &#x7B49;&#x5F85;&#x68C0;&#x67E5;&#xFF0C;7&#x5404;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x540E;&#xFF0C;&#x6267;&#x884C;await&#x540E;&#x9762;&#x7684;&#x4EE3;&#x7801;</span>
        latch<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x96C6;&#x9F50;&#x4E03;&#x9897;&#x9F99;&#x73E0;&#xFF01;&#x53EC;&#x5524;&#x795E;&#x9F99;&#xFF01;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF1A;</p>
<ol>
<li>CountDownLatch&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x8868;&#x793A;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x91CF;&#xFF0C;&#x53EA;&#x80FD;&#x8BBE;&#x7F6E;&#x4E00;&#x6B21;</li>
<li>&#x5728;&#x6BCF;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x4E4B;&#x540E;&#xFF0C;&#x90FD;&#x9700;&#x8981;&#x6267;&#x884C;countDownLatch.countDown()&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x7136;&#x8BA1;&#x6570;&#x4E0D;&#x51C6;&#x786E;</li>
<li>&#x53EA;&#x6709;&#x6240;&#x6709;&#x7684;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x4E4B;&#x540E;&#xFF0C;&#x624D;&#x4F1A;&#x6267;&#x884C;countDownLatch.await()&#x4E4B;&#x540E;&#x7684;&#x4EE3;&#x7801;</li>
<li>countDownLatch&#x963B;&#x585E;&#x7684;&#x662F;&#x4E3B;&#x7EBF;&#x7A0B;</li>
</ol>
<blockquote>
<p>CountDownLatch&#x4F7F;&#x7528;&#x573A;&#x666F;</p>
</blockquote>
<p>&#xFF08;1&#xFF09;&#x5B9E;&#x73B0;&#x6700;&#x5927;&#x7684;&#x5E76;&#x884C;&#x6027;&#xFF1A;&#x540C;&#x65F6;&#x542F;&#x52A8;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#xFF0C;&#x5B9E;&#x73B0;&#x6700;&#x5927;&#x7A0B;&#x5EA6;&#x7684;&#x5E76;&#x884C;&#x6027;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x6D4B;&#x8BD5;&#x4E00;&#x4E2A;&#x5355;&#x4F8B;&#x7C7B;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x521D;&#x59CB;&#x8BA1;&#x6570;&#x4E3A;1&#x7684;CountDownLatch&#xFF0C;&#x5E76;&#x8BA9;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x90FD;&#x5728;&#x8FD9;&#x4E2A;&#x9501;&#x4E0A;&#x7B49;&#x5F85;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5F88;&#x8F7B;&#x677E;&#x5730;&#x5B8C;&#x6210;&#x6D4B;&#x8BD5;&#x3002;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x8C03;&#x7528;&#x4E00;&#x6B21;countDown()
&#x65B9;&#x6CD5;&#x5C31;&#x53EF;&#x4EE5;&#x8BA9;&#x6240;&#x6709;&#x7684;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x6062;&#x590D;&#x6267;&#x884C;</p>
<p>&#xFF08;2&#xFF09;&#x5F00;&#x59CB;&#x6267;&#x884C;&#x524D;&#x7B49;&#x5F85;n&#x4E2A;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x5404;&#x81EA;&#x4EFB;&#x52A1;&#xFF1A;&#x4F8B;&#x5982;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x542F;&#x52A8;&#x7C7B;&#x8981;&#x786E;&#x4FDD;&#x5728;&#x5904;&#x7406;&#x7528;&#x6237;&#x8BF7;&#x6C42;&#x524D;&#xFF0C;&#x6240;&#x6709;N&#x4E2A;&#x5916;&#x90E8;&#x7CFB;&#x7EDF;&#x5DF2;&#x7ECF;&#x542F;&#x52A8;&#x548C;&#x8FD0;&#x884C;&#x4E86;</p>
<p>&#xFF08;3&#xFF09;&#x6B7B;&#x9501;&#x68C0;&#x6D4B;&#xFF1A;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x65B9;&#x4FBF;&#x7684;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x662F;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;n&#x4E2A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x5171;&#x4EAB;&#x8D44;&#x6E90;&#xFF0C;&#x5728;&#x6BCF;&#x6B21;&#x6D4B;&#x8BD5;&#x9636;&#x6BB5;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x76EE;&#x662F;&#x4E0D;&#x540C;&#x7684;&#xFF0C;&#x5E76;&#x5C1D;&#x8BD5;&#x4EA7;&#x751F;&#x6B7B;&#x9501;</p>
<h2 id="cyclicbarrier"><a name="cyclicbarrier" class="anchor-navigation-ex-anchor" href="#cyclicbarrier"><i class="fa fa-link" aria-hidden="true"></i></a>2.15. CyclicBarrier</h2>
<p>CyclicBarrier&#x662F;&#x53E6;&#x4E00;&#x79CD;&#x591A;&#x7EBF;&#x7A0B;&#x5E76;&#x53D1;&#x63A7;&#x5236;&#x4F7F;&#x7528;&#x5DE5;&#x5177;&#xFF0C;&#x548C;CountDownLatch&#x975E;&#x5E38;&#x76F8;&#x4F3C;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x7EBF;&#x7A0B;&#x95F4;&#x7684;&#x8BA1;&#x6570;&#x7B49;&#x5F85;&#xFF0C;&#x4F46;&#x4ED6;&#x7684;&#x529F;&#x80FD;&#x8981;&#x6BD4;CountDownLatch&#x66F4;&#x52A0;&#x5F3A;&#x5927;</p>
<p>CyclicBarrier&#x5B57;&#x9762;&#x610F;&#x601D;&#x662F;&#x53EF;&#x5FAA;&#x73AF;&#x4F7F;&#x7528;&#xFF08;Cyclic&#xFF09;&#x7684;&#x5C4F;&#x969C;&#xFF08;Barrier&#xFF09;&#x3002;&#x5B83;&#x7684;&#x4F5C;&#x7528;&#x662F;&#xFF0C;&#x8BA9;&#x4E00;&#x7EC4;&#x7EBF;&#x7A0B;&#x8FBE;&#x5230;&#x4E00;&#x4E2A;&#x5C4F;&#x969C;&#xFF08;&#x4E5F;&#x53EB;&#x540C;&#x6B65;&#x70B9;&#xFF09;&#x65F6;&#x88AB;&#x963B;&#x585E;&#xFF0C;&#x76F4;&#x5230;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x8FBE;&#x5230;&#x5C4F;&#x969C;&#x65F6;&#xFF0C;&#x5C4F;&#x969C;&#x624D;&#x4F1A;&#x5F00;&#x95E8;&#xFF0C;&#x6240;&#x6709;&#x88AB;&#x5C4F;&#x969C;&#x62E6;&#x622A;&#x7684;&#x7EBF;&#x7A0B;&#x624D;&#x4F1A;&#x7EE7;&#x7EED;&#x5E72;&#x6D3B;</p>
<p>CyclicBarrier&#x9ED8;&#x8BA4;&#x7684;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x662F;CyclicBarrier(int parties)&#xFF0C;&#x5176;&#x53C2;&#x6570;&#x8868;&#x793A;&#x5C4F;&#x969C;&#x62E6;&#x622A;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x91CF;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;await()
&#x65B9;&#x6CD5;&#x540E;&#x544A;&#x8BC9;CyclicBarrier&#x5DF2;&#x7ECF;&#x8FBE;&#x5230;&#x4E86;&#x5C4F;&#x969C;&#xFF0C;&#x7136;&#x540E;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x88AB;&#x963B;&#x585E;</p>
<p>CyclicBarrier&#x5F3A;&#x8C03;&#x7684;&#x662F;n&#x4E2A;&#x7EBF;&#x7A0B;&#xFF0C;&#x5927;&#x5BB6;&#x76F8;&#x4E92;&#x7B49;&#x5F85;&#xFF0C;&#x53EA;&#x8981;&#x6709;&#x4E00;&#x4E2A;&#x6CA1;&#x5B8C;&#x6210;&#xFF0C;&#x6240;&#x6709;&#x4EBA;&#x90FD;&#x5F97;&#x7B49;&#x7740;</p>
<p>CyclicBarrier&#x53EF;&#x4EE5;&#x4F7F;&#x7528;reset&#x6062;&#x590D;&#x5230;&#x6700;&#x521D;&#x59CB;&#x7684;&#x72B6;&#x6001;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">int</span> <span class="token constant">COUNT</span> <span class="token operator">=</span> <span class="token number">7</span><span class="token punctuation">;</span>

    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x7B2C;&#x4E8C;&#x4E2A;&#x5C4F;&#x969C;barrier&#xFF1A;&#x5BFB;&#x627E;&#x9F99;&#x73E0;</span>
        <span class="token class-name">CyclicBarrier</span> cyclicBarrier2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CyclicBarrier</span><span class="token punctuation">(</span><span class="token constant">COUNT</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x96C6;&#x9F50;&#x4E03;&#x9897;&#x9F99;&#x73E0;&#xFF0C;&#x53EC;&#x5524;&#x795E;&#x9F99;&#xFF01;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token constant">COUNT</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x96C6;&#x9F50;&#x7B2C;&quot;</span> <span class="token operator">+</span> index <span class="token operator">+</span> <span class="token string">&quot;&#x8BFE;&#x9F99;&#x73E0;&#xFF01;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    cyclicBarrier2<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> <span class="token operator">|</span> <span class="token class-name">BrokenBarrierException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x7B2C;&#x4E00;&#x4E2A;&#x5C4F;&#x969C;barrier&#xFF1A;&#x53EC;&#x96C6;&#x6CD5;&#x5E08;</span>
        <span class="token class-name">CyclicBarrier</span> cyclicBarrier1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CyclicBarrier</span><span class="token punctuation">(</span><span class="token constant">COUNT</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6CD5;&#x5E08;&#x53EC;&#x96C6;&#x5B8C;&#x6BD5;&#xFF0C;&#x540C;&#x65F6;&#x51FA;&#x53D1;&#xFF0C;&#x53BB;&#x4E0D;&#x540C;&#x5730;&#x65B9;&#x5BFB;&#x627E;&#x9F99;&#x73E0;&#xFF01;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token constant">COUNT</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x53EC;&#x96C6;&#x7B2C;&quot;</span> <span class="token operator">+</span> index <span class="token operator">+</span> <span class="token string">&quot;&#x4E2A;&#x6CD5;&#x5E08;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    cyclicBarrier1<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> <span class="token operator">|</span> <span class="token class-name">BrokenBarrierException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x662F;&#x6284;&#x7684;&#xFF0C;&#x4E2A;&#x4EBA;&#x89C9;&#x5F97;&#x4E0D;&#x5BF9;&#xFF0C;&#x56E0;&#x4E3A;main&#x548C;test&#x4E2D;&#x7684;&#x7EBF;&#x7A0B;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;</p>
<p>CyclicBarrier&#x662F;&#x5BF9;&#x6307;&#x5B9A;&#x4E2A;&#x6570;&#x7684;&#x6240;&#x6709;&#x5B50;&#x7EBF;&#x7A0B;&#x52A0;&#x62E6;&#x622A;&#xFF0C;&#x6240;&#x6709;&#x5B50;&#x7EBF;&#x7A0B;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x6302;&#x8D77;&#x540E;&#x53BB;&#x9664;&#x62E6;&#x622A;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x5404;&#x5B50;&#x7EBF;&#x7A0B;&#x540E;&#x9762;&#x7684;&#x4EE3;&#x7801;</p>
<p>CyclicBarrier&#x662F;&#x4E3B;&#x7EBF;&#x7A0B;&#x63A7;&#x5236;&#x5B50;&#x7EBF;&#x7A0B;&#x7684;&#x884C;&#x4E3A;&#xFF1A;&#x5B50;&#x7EBF;&#x7A0B;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x540E;&#x6302;&#x8D77;&#xFF0C;&#x6240;&#x6709;&#x5B50;&#x7EBF;&#x7A0B;&#x6302;&#x8D77;&#x540E;&#x53BB;&#x6389;&#x62E6;&#x622A;&#x7EE7;&#x7EED;&#x6267;&#x884C;</p>
<p>CountDownLatch&#x662F;&#x5B50;&#x7EBF;&#x7A0B;&#x63A7;&#x5236;&#x4E3B;&#x7EBF;&#x7A0B;&#x7684;&#x884C;&#x4E3A;&#xFF1A;&#x5B50;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x7ED3;&#x675F;&#x540E;&#x4E3B;&#x7EBF;&#x7A0B;&#x5F97;&#x4EE5;&#x6267;&#x884C;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">// &#x81EA;&#x5DF1;&#x5199;&#x7684;&#x6D4B;&#x8BD5;&#x5B50;&#x7EBF;&#x7A0B;&#x7684;&#x4E2D;&#x65AD;&#x548C;&#x6267;&#x884C;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">int</span> <span class="token constant">COUNT</span> <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">CyclicBarrier</span> cyclicBarrier1 <span class="token operator">=</span>
                <span class="token keyword">new</span> <span class="token class-name">CyclicBarrier</span><span class="token punctuation">(</span><span class="token constant">COUNT</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7B2C;&#x4E00;&#x4E2A;&#x62E6;&#x622A;&#x7ED3;&#x675F;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">CyclicBarrier</span> cyclicBarrier2 <span class="token operator">=</span>
                <span class="token keyword">new</span> <span class="token class-name">CyclicBarrier</span><span class="token punctuation">(</span><span class="token constant">COUNT</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7B2C;&#x4E8C;&#x4E2A;&#x62E6;&#x622A;&#x7ED3;&#x675F;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token constant">COUNT</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7EBF;&#x7A0B;&#x88AB;&#x6302;&#x8D77;:&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    cyclicBarrier1<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7EBF;&#x7A0B;&#x7EE7;&#x7EED;&#x6267;&#x884C;:&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    cyclicBarrier2<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7EBF;&#x7A0B;&#x7B2C;&#x4E8C;&#x6B21;&#x6267;&#x884C;:&quot;</span> <span class="token operator">+</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> <span class="token operator">|</span> <span class="token class-name">BrokenBarrierException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">&quot;thread&quot;</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>CountDownLatch&#x4E0E;CyclicBarrier&#x7684;&#x533A;&#x522B;</p>
</blockquote>
<p>&#xFF08;1&#xFF09;CountDownLatch&#x7684;&#x8BA1;&#x6570;&#x5668;&#x53EA;&#x80FD;&#x4F7F;&#x7528;&#x4E00;&#x6B21;&#xFF0C;CyclicBarrier&#x7684;&#x8BA1;&#x6570;&#x5668;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;reset()
&#x65B9;&#x6CD5;&#x91CD;&#x7F6E;&#x3002;&#x6240;&#x4EE5;CyclicBarrier&#x80FD;&#x5904;&#x7406;&#x66F4;&#x4E3A;&#x590D;&#x6742;&#x7684;&#x4E1A;&#x52A1;&#x573A;&#x666F;&#xFF0C;&#x6BD4;&#x5982;&#x5982;&#x679C;&#x8BA1;&#x7B97;&#x53D1;&#x751F;&#x9519;&#x8BEF;&#xFF0C;&#x53EF;&#x4EE5;&#x91CD;&#x7F6E;&#x8BA1;&#x6570;&#x5668;&#xFF0C;&#x5E76;&#x8BA9;&#x7EBF;&#x7A0B;&#x4EEC;&#x91CD;&#x65B0;&#x6267;&#x884C;&#x4E00;&#x6B21;</p>
<p>&#xFF08;2&#xFF09;CyclicBarrier&#x8FD8;&#x63D0;&#x4F9B;&#x5176;&#x4ED6;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x6BD4;&#x5982;getNumberWaiting&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x83B7;&#x5F97;CyclicBarrier&#x963B;&#x585E;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x91CF;&#x3002;isBroken&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x77E5;&#x9053;&#x963B;&#x585E;&#x7684;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x88AB;&#x4E2D;&#x65AD;</p>
<p>&#xFF08;3&#xFF09;CountDownLatch&#x4F1A;&#x963B;&#x585E;&#x4E3B;&#x7EBF;&#x7A0B;&#xFF0C;CyclicBarrier&#x4E0D;&#x4F1A;&#x963B;&#x585E;&#x4E3B;&#x7EBF;&#x7A0B;&#xFF0C;&#x53EA;&#x4F1A;&#x963B;&#x585E;&#x5B50;&#x7EBF;&#x7A0B;</p>
<h2 id="&#x7EBF;&#x7A0B;&#x6C60;"><a name="&#x7EBF;&#x7A0B;&#x6C60;" class="anchor-navigation-ex-anchor" href="#&#x7EBF;&#x7A0B;&#x6C60;"><i class="fa fa-link" aria-hidden="true"></i></a>2.16. &#x7EBF;&#x7A0B;&#x6C60;</h2>
<p>&#x591A;&#x7EBF;&#x7A0B;&#x6280;&#x672F;&#x53EF;&#x4EE5;&#x5145;&#x5206;&#x53D1;&#x6325;&#x591A;&#x6838;&#x5904;&#x7406;&#x5668;&#x7684;&#x8BA1;&#x7B97;&#x80FD;&#x529B;&#xFF0C;&#x63D0;&#x9AD8;&#x751F;&#x4EA7;&#x7CFB;&#x7EDF;&#x7684;&#x541E;&#x5410;&#x91CF;&#x548C;&#x6027;&#x80FD;&#xFF0C;&#x4F46;&#x662F;&#x82E5;&#x4E0D;&#x52A0;&#x63A7;&#x5236;&#x548C;&#x7BA1;&#x7406;&#x7684;&#x968F;&#x610F;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#xFF0C;&#x5BF9;&#x7CFB;&#x7EDF;&#x7684;&#x6027;&#x80FD;&#x53CD;&#x800C;&#x4F1A;&#x4EA7;&#x751F;&#x4E0D;&#x5229;&#x7684;&#x5F71;&#x54CD;&#x3002;&#x7EBF;&#x7A0B;&#x662F;&#x4E00;&#x79CD;&#x8F7B;&#x91CF;&#x7EA7;&#x7684;&#x5DE5;&#x5177;&#xFF08;&#x8F7B;&#x91CF;&#x7EA7;&#x8FDB;&#x7A0B;&#xFF09;&#xFF0C;&#x4F46;&#x5176;&#x521B;&#x5EFA;&#x548C;&#x5173;&#x95ED;&#x4F9D;&#x7136;&#x8981;&#x82B1;&#x8D39;&#x65F6;&#x95F4;&#xFF0C;&#x5982;&#x679C;&#x4E3A;&#x4E86;&#x4E00;&#x4E2A;&#x5F88;&#x7B80;&#x5355;&#x7684;&#x4EFB;&#x52A1;&#x53BB;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#xFF0C;&#x5F88;&#x6709;&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x521B;&#x5EFA;&#x548C;&#x9500;&#x6BC1;&#x7EBF;&#x7A0B;&#x5360;&#x7528;&#x7684;&#x65F6;&#x95F4;&#x5927;&#x4E8E;&#x8BE5;&#x7EBF;&#x7A0B;&#x771F;&#x5B9E;&#x5DE5;&#x4F5C;&#x6D88;&#x8017;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x53CD;&#x800C;&#x5F97;&#x4E0D;&#x507F;&#x5931;</p>
<blockquote>
<p>&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x7279;&#x70B9;</p>
</blockquote>
<ol>
<li>&#x964D;&#x4F4E;&#x8D44;&#x6E90;&#x6D88;&#x8017;&#x3002;&#x901A;&#x8FC7;&#x91CD;&#x590D;&#x5229;&#x7528;&#x81EA;&#x5DF1;&#x521B;&#x5EFA;&#x7684;&#x7EBF;&#x7A0B;&#x964D;&#x4F4E;&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;&#x548C;&#x9500;&#x6BC1;&#x9020;&#x6210;&#x7684;&#x6D88;&#x8017;</li>
<li>&#x63D0;&#x9AD8;&#x76F8;&#x5E94;&#x901F;&#x5EA6;&#x3002;&#x5F53;&#x4EFB;&#x52A1;&#x8FBE;&#x5230;&#x65F6;&#xFF0C;&#x4EFB;&#x52A1;&#x53EF;&#x4EE5;&#x4E0D;&#x9700;&#x8981;&#x7B49;&#x5230;&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;&#x5C31;&#x80FD;&#x7ACB;&#x5373;&#x6267;&#x884C;</li>
<li>&#x63D0;&#x9AD8;&#x7EBF;&#x7A0B;&#x7684;&#x53EF;&#x7BA1;&#x7406;&#x6027;&#x3002;&#x7EBF;&#x7A0B;&#x65F6;&#x7A00;&#x7F3A;&#x8D44;&#x6E90;&#xFF0C;&#x5982;&#x679C;&#x65E0;&#x9650;&#x5236;&#x5730;&#x521B;&#x5EFA;&#xFF0C;&#x4E0D;&#x4EC5;&#x4F1A;&#x6D88;&#x8017;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#xFF0C;&#x8FD8;&#x4F1A;&#x964D;&#x4F4E;&#x7CFB;&#x7EDF;&#x7684;&#x7A33;&#x5B9A;&#x6027;&#xFF0C;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x7EDF;&#x4E00;&#x5206;&#x914D;&#x3001;&#x8C03;&#x4F18;&#x548C;&#x76D1;&#x63A7;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x8981;&#x505A;&#x5230;&#x5408;&#x7406;&#x5229;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;&#x5FC5;&#x987B;&#x5BF9;&#x9F50;&#x5B9E;&#x73B0;&#x539F;&#x7406;&#x4E86;&#x5982;&#x6307;&#x638C;</li>
</ol>
<p>JDK&#x63D0;&#x4F9B;&#x4E86;Executor&#x6846;&#x67B6;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x6709;&#x6548;&#x7684;&#x7BA1;&#x7406;&#x548C;&#x63A7;&#x5236;&#x6211;&#x4EEC;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x5176;&#x5B9E;&#x8D28;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6C60;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">// Executors&#x5176;&#x672C;&#x8D28;&#x662F;&#x8C03;&#x7528;ThreadPoolExecutor</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Executors</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x56FA;&#x5B9A;&#x7EBF;&#x7A0B;&#x6570;&#x91CF;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">ExecutorService</span> newFixedThreadPool
    <span class="token comment">// &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">ExecutorService</span> newSingleThreadExecutor
    <span class="token comment">// &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x5B9E;&#x9645;&#x60C5;&#x51B5;&#x8C03;&#x6574;&#x6570;&#x91CF;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">ExecutorService</span> newCachedThreadPool
    <span class="token comment">// &#x6BD4;newSingleThreadExecutor&#x7ED9;&#x5B9A;&#x4E86;&#x65F6;&#x95F4;&#x6267;&#x884C;&#x67D0;&#x4EFB;&#x52A1;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x5B9A;&#x65F6;&#x6267;&#x884C;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">ScheduledExecutorService</span> newSingleThreadScheduledExecutor
    <span class="token comment">// &#x5728;newSingleThreadScheduledExecutor&#x7684;&#x57FA;&#x7840;&#x4E0A;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x7EBF;&#x7A0B;&#x6570;&#x91CF;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">ScheduledExecutorService</span> newScheduledThreadPool
<span class="token punctuation">}</span>
</code></pre>
<p>ThreadPoolExecutor&#x6784;&#x9020;&#x65B9;&#x6CD5;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">/*
corePoolSize    &#x6838;&#x5FC3;&#x7EBF;&#x7A0B;&#x6C60;&#x5927;&#x5C0F;
maximumPoolSize    &#x7EBF;&#x7A0B;&#x6C60;&#x6700;&#x5927;&#x5BB9;&#x91CF;&#x5927;&#x5C0F;
keepAliveTime    &#x7EBF;&#x7A0B;&#x6C60;&#x7A7A;&#x95F2;&#x65F6;&#xFF0C;&#x7EBF;&#x7A0B;&#x5B58;&#x6D3B;&#x7684;&#x65F6;&#x95F4;
TimeUnit        &#x65F6;&#x95F4;&#x5355;&#x4F4D;
ThreadFactory    &#x7EBF;&#x7A0B;&#x5DE5;&#x5382;
BlockingQueue    &#x4EFB;&#x52A1;&#x961F;&#x5217;
RejectedExecutionHandler    &#x7EBF;&#x7A0B;&#x62D2;&#x7EDD;&#x7B56;&#x7565;            
*/</span>
<span class="token keyword">public</span> <span class="token class-name">ThreadPoolExecutor</span><span class="token punctuation">(</span><span class="token keyword">int</span> corePoolSize<span class="token punctuation">,</span>
        <span class="token keyword">int</span> maximumPoolSize<span class="token punctuation">,</span>
        <span class="token keyword">long</span> keepAliveTime<span class="token punctuation">,</span>
        <span class="token class-name">TimeUnit</span> unit<span class="token punctuation">,</span>
        <span class="token class-name">BlockingQueue</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Runnable</span><span class="token punctuation">&gt;</span></span> workQueue<span class="token punctuation">,</span>
        <span class="token class-name">ThreadFactory</span> threadFactory<span class="token punctuation">,</span>
        <span class="token class-name">RejectedExecutionHandler</span> handler<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>corePoolSize<span class="token operator">&lt;</span> <span class="token number">0</span><span class="token operator">||</span>
        maximumPoolSize<span class="token operator">&lt;=</span><span class="token number">0</span><span class="token operator">||</span>
        maximumPoolSize<span class="token operator">&lt;</span>corePoolSize <span class="token operator">||</span>
        keepAliveTime<span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">IllegalArgumentException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>workQueue<span class="token operator">==</span><span class="token keyword">null</span><span class="token operator">||</span>threadFactory<span class="token operator">==</span><span class="token keyword">null</span><span class="token operator">||</span>handler<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">NullPointerException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>corePoolSize<span class="token operator">=</span>corePoolSize<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>maximumPoolSize<span class="token operator">=</span>maximumPoolSize<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>workQueue<span class="token operator">=</span>workQueue<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>keepAliveTime<span class="token operator">=</span>unit<span class="token punctuation">.</span><span class="token function">toNanos</span><span class="token punctuation">(</span>keepAliveTime<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>threadFactory<span class="token operator">=</span>threadFactory<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>handler<span class="token operator">=</span>handler<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x5B9E;&#x4F8B;&#x4E00;</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ExecutorService</span> executorService <span class="token operator">=</span> <span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">newFixedThreadPool</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            executorService<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;i:&quot;</span> <span class="token operator">+</span> index <span class="token operator">+</span> <span class="token string">&quot; executorService&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        executorService<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>submit(Runnable task)&#x65B9;&#x6CD5;&#x63D0;&#x4EA4;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;</p>
<p>&#x963F;&#x91CC;&#x7F16;&#x7801;&#x89C4;&#x8303;&#x4E2D;&#x4E0D;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;Executors&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#xFF0C;&#x800C;&#x662F;&#x4F7F;&#x7528;ThreadPoolExecutor&#x521B;&#x5EFA;</p>
<p>&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x907F;&#x514D;&#x7531;&#x4E8E;&#x5BF9;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x8FD0;&#x884C;&#x673A;&#x5236;&#x4E0D;&#x4E86;&#x89E3;&#x800C;&#x5F15;&#x8D77;&#x8D44;&#x6E90;&#x8017;&#x5C3D;&#x7684;&#x98CE;&#x9669;</p>
<pre class="language-"><code>Executors&#x5404;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x5F0A;&#x7AEF;&#xFF1A;
1&#xFF09;newFixedThreadPool&#x548C;newSingleThreadExecutor:
&#x2003;&#x2003;&#x4E3B;&#x8981;&#x95EE;&#x9898;&#x662F;&#x5806;&#x79EF;&#x7684;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x961F;&#x5217;&#x53EF;&#x80FD;&#x4F1A;&#x8017;&#x8D39;&#x975E;&#x5E38;&#x5927;&#x7684;&#x5185;&#x5B58;&#xFF0C;&#x751A;&#x81F3;OOM&#x3002;
2&#xFF09;newCachedThreadPool&#x548C;newScheduledThreadPool:
&#x2003;&#x2003;&#x4E3B;&#x8981;&#x95EE;&#x9898;&#x662F;&#x7EBF;&#x7A0B;&#x6570;&#x6700;&#x5927;&#x6570;&#x662F;Integer.MAX_VALUE&#xFF0C;&#x53EF;&#x80FD;&#x4F1A;&#x521B;&#x5EFA;&#x6570;&#x91CF;&#x975E;&#x5E38;&#x591A;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x751A;&#x81F3;OOM&#x3002;
</code></pre><blockquote>
<p>&#x5B9E;&#x4F8B;&#x4E8C;</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token comment">// ExecutorService</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ExecutorService</span> executorService <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadPoolExecutor</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0L</span><span class="token punctuation">,</span>
                <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">MILLISECONDS</span><span class="token punctuation">,</span>
                <span class="token keyword">new</span> <span class="token class-name">LinkedBlockingDeque</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">defaultThreadFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token keyword">new</span> <span class="token class-name">ThreadPoolExecutor<span class="token punctuation">.</span>AbortPolicy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            executorService<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;i:&quot;</span> <span class="token operator">+</span> index <span class="token operator">+</span> <span class="token string">&quot; executorService&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        executorService<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// ThreadPoolExecutor</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ThreadPoolExecutor</span> pool <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadPoolExecutor</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0L</span><span class="token punctuation">,</span>
                <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">MILLISECONDS</span><span class="token punctuation">,</span>
                <span class="token keyword">new</span> <span class="token class-name">LinkedBlockingDeque</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">defaultThreadFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token keyword">new</span> <span class="token class-name">ThreadPoolExecutor<span class="token punctuation">.</span>AbortPolicy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            pool<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;i:&quot;</span> <span class="token operator">+</span> index <span class="token operator">+</span> <span class="token string">&quot; executorService&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        pool<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x5B9E;&#x4F8B;&#x4E09;</p>
</blockquote>
<pre class="language-"><code class="lang-java"><span class="token comment">// &#x81EA;&#x5B9A;&#x4E49;ThreadFactory</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ExecutorService</span> executorService <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadPoolExecutor</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">0L</span><span class="token punctuation">,</span>
                <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">MILLISECONDS</span><span class="token punctuation">,</span>
                <span class="token keyword">new</span> <span class="token class-name">LinkedBlockingDeque</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token keyword">new</span> <span class="token class-name">ThreadFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token annotation punctuation">@Override</span>
                    <span class="token keyword">public</span> <span class="token class-name">Thread</span> <span class="token function">newThread</span><span class="token punctuation">(</span><span class="token class-name">Runnable</span> r<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token class-name">Thread</span> thread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        thread<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span>r<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">return</span> thread<span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span>
                <span class="token keyword">new</span> <span class="token class-name">ThreadPoolExecutor<span class="token punctuation">.</span>AbortPolicy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            executorService<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;i:&quot;</span> <span class="token operator">+</span> index <span class="token operator">+</span> <span class="token string">&quot; executorService&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        executorService<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>submit&#x7684;&#x5751;</p>
</blockquote>
<p>&#x4F7F;&#x7528;submit(Runnable task)&#x65F6;100/0&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;java.lang.ArithmeticException: / by zero&#x4E0D;&#x663E;&#x793A;</p>
<p>&#x56E0;&#x4E3A;&#x9519;&#x8BEF;&#x7684;&#x5806;&#x6808;&#x4FE1;&#x606F;&#x8DD1;&#x51FA;&#x6765;&#x7684;&#x65F6;&#x5019;&#x4F1A;&#x88AB;&#x5185;&#x90E8;&#x6355;&#x83B7;&#x5230;&#xFF0C;&#x6240;&#x4EE5;&#x6253;&#x5370;&#x4E0D;&#x51FA;&#x5177;&#x4F53;&#x7684;&#x4FE1;&#x606F;&#x8BA9;&#x6211;&#x4EEC;&#x67E5;&#x770B;&#xFF0C;&#x53EF;&#x4F7F;&#x7528;</p>
<ul>
<li>execute()</li>
<li>Future</li>
</ul>
<pre class="language-"><code class="lang-java"><span class="token comment">// &#x4F7F;&#x7528;submit</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">divTask</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">double</span> result <span class="token operator">=</span> a <span class="token operator">/</span> b<span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ExecutorService</span> executorService <span class="token operator">=</span> <span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">newFixedThreadPool</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            executorService<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">divTask</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> index<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        executorService<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x4F7F;&#x7528;execute</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">divTask</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">double</span> result <span class="token operator">=</span> a <span class="token operator">/</span> b<span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ExecutorService</span> executorService <span class="token operator">=</span> <span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">newFixedThreadPool</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            executorService<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">divTask</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> index<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        executorService<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x4F7F;&#x7528;Future</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">divTask</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">double</span> result <span class="token operator">=</span> a <span class="token operator">/</span> b<span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">ExecutorService</span> executorService <span class="token operator">=</span> <span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">newFixedThreadPool</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> index <span class="token operator">=</span> i<span class="token punctuation">;</span>
            <span class="token class-name">Future</span> future <span class="token operator">=</span> executorService<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">divTask</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> index<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                future<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> <span class="token operator">|</span> <span class="token class-name">ExecutionException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        executorService<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>execute&#x548C;submit&#x533A;&#x522B;</p>
</blockquote>
<p>&#xFF08;1&#xFF09;execute()&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x63D0;&#x4EA4;&#x4E0D;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x6240;&#x4EE5;&#x65E0;&#x6CD5;&#x5224;&#x65AD;&#x4EFB;&#x52A1;&#x662F;&#x5426;&#x88AB;&#x7EBF;&#x7A0B;&#x6C60;&#x6267;&#x884C;&#x6210;&#x529F;&#x3002;</p>
<p>&#xFF08;2&#xFF09;submit()
&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x63D0;&#x4EA4;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x7EBF;&#x7A0B;&#x6C60;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;Future&#x7C7B;&#x578B;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x901A;&#x8FC7;Future&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x5224;&#x65AD;&#x4EFB;&#x52A1;&#x662F;&#x5426;&#x6267;&#x884C;&#x6210;&#x529F;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;future&#x7684;get()
&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;get()&#x65B9;&#x6CD5;&#x4F1A;&#x963B;&#x585E;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x76F4;&#x5230;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#xFF0C;&#x800C;&#x4F7F;&#x7528;get(long timeout, TimeUnit unit)
&#x65B9;&#x6CD5;&#x5219;&#x4F1A;&#x963B;&#x585E;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x540E;&#x7ACB;&#x5373;&#x8FD4;&#x56DE;&#xFF0C;&#x8FD9;&#x65F6;&#x5019;&#x6709;&#x53EF;&#x80FD;&#x4EFB;&#x52A1;&#x6CA1;&#x6709;&#x6267;&#x884C;&#x5B8C;</p>
<h2 id="threadlocal"><a name="threadlocal" class="anchor-navigation-ex-anchor" href="#threadlocal"><i class="fa fa-link" aria-hidden="true"></i></a>2.17. ThreadLocal</h2>
<blockquote>
<p>ThreadLocal&#x7B80;&#x4ECB;</p>
</blockquote>
<p>&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x7528;ThreadLocal&#x7ED1;&#x5B9A;&#x81EA;&#x5DF1;&#x7684;&#x53D8;&#x91CF;&#x3002;&#x4F7F;&#x7528;ThreadLocal&#x7EF4;&#x62A4;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;ThreadLocal&#x4E3A;&#x6BCF;&#x4E2A;&#x8BE5;&#x53D8;&#x91CF;&#x7684;&#x7EBF;&#x7A0B;&#x63D0;&#x4F9B;&#x72EC;&#x7ACB;&#x7684;&#x53D8;&#x91CF;&#x526F;&#x672C;&#xFF0C;&#x6240;&#x4EE5;&#x6BCF;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x90FD;&#x53EF;&#x4EE5;&#x72EC;&#x7ACB;&#x5730;&#x6539;&#x53D8;&#x81EA;&#x5DF1;&#x7684;&#x526F;&#x672C;&#xFF0C;&#x800C;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x526F;&#x672C;&#x3002;</p>
<blockquote>
<p>ThreadLocal&#x4E0E;synchronized</p>
</blockquote>
<p>synchronized&#x901A;&#x8FC7;&#x5BF9;&#x8C61;&#x7684;&#x9501;&#x673A;&#x5236;&#x4FDD;&#x8BC1;&#x540C;&#x4E00;&#x65F6;&#x95F4;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x65F6;&#x8BE5;&#x53D8;&#x91CF;&#x662F;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x5171;&#x4EAB;&#x7684;&#xFF0C;&#x9700;&#x8981;&#x8003;&#x8651;&#x5BF9;&#x8C61;&#x8BFB;&#x5199;&#x3001;&#x9501;&#x5B9A;&#x3001;&#x91CA;&#x653E;&#x7B49;&#x95EE;&#x9898;&#xFF0C;&#x8BBE;&#x8BA1;&#x548C;&#x7F16;&#x5199;&#x96BE;&#x5EA6;&#x8F83;&#x5927;&#x3002;</p>
<p>ThreadLocal&#x662F;&#x7EBF;&#x7A0B;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF0C;&#x662F;&#x4E00;&#x79CD;&#x591A;&#x7EBF;&#x7A0B;&#x95F4;&#x5E76;&#x53D1;&#x8BBF;&#x95EE;&#x7684;&#x91CF;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x3002;Threadlocal&#x4E0D;&#x63D0;&#x4F9B;&#x9501;&#xFF0C;&#x662F;&#x4EE5;&#x7A7A;&#x95F4;&#x6362;&#x65F6;&#x95F4;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x4E3A;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x63D0;&#x4F9B;&#x53D8;&#x91CF;&#x7684;&#x72EC;&#x7ACB;&#x526F;&#x672C;&#xFF0C;&#x4EE5;&#x4FDD;&#x8BC1;&#x7EBF;&#x7A0B;&#x7684;&#x5B89;&#x5168;</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Thread</span><span class="token punctuation">,</span> <span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> valueMap <span class="token operator">=</span> <span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">synchronizedMap</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Thread</span><span class="token punctuation">,</span> <span class="token class-name">T</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">set</span><span class="token punctuation">(</span><span class="token class-name">T</span> value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        valueMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">T</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Thread</span> currentThread <span class="token operator">=</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">T</span> t <span class="token operator">=</span> valueMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>currentThread<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>t <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>valueMap<span class="token punctuation">.</span><span class="token function">containsKey</span><span class="token punctuation">(</span>currentThread<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            t <span class="token operator">=</span> <span class="token function">initialValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            valueMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>currentThread<span class="token punctuation">,</span> t<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> t<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        valueMap<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">T</span> <span class="token function">initialValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Test</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> test <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
            <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> params <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            params<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;&#x5F20;&#x4E09;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            params<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;&#x674E;&#x56DB;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            params<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;&#x738B;&#x4E94;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            test<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            test<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> params <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                params<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                params<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;cat&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                test<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                test<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h1 id="thread"><a name="thread" class="anchor-navigation-ex-anchor" href="#thread"><i class="fa fa-link" aria-hidden="true"></i></a>3. Thread</h1>
<h2 id="thread&#x542F;&#x52A8;&#x539F;&#x7406;"><a name="thread&#x542F;&#x52A8;&#x539F;&#x7406;" class="anchor-navigation-ex-anchor" href="#thread&#x542F;&#x52A8;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1. Thread&#x542F;&#x52A8;&#x539F;&#x7406;</h2>
<h3 id="start&#x65B9;&#x6CD5;"><a name="start&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#start&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1.1. start()&#x65B9;&#x6CD5;</h3>
<pre class="language-"><code class="lang-java"><span class="token comment">// &#x521B;&#x5EFA;&#x5E76;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;</span>
<span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token punctuation">{</span><span class="token keyword">do</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// start&#x6E90;&#x7801;</span>
<span class="token keyword">public</span> <span class="token keyword">synchronized</span> <span class="token keyword">void</span> <span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>threadStatus<span class="token operator">!=</span><span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">IllegalThreadStateException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        group<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">boolean</span> started<span class="token operator">=</span><span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span><span class="token punctuation">{</span>
        <span class="token function">start0</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        started<span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">finally</span><span class="token punctuation">{</span>
        <span class="token keyword">try</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>started<span class="token punctuation">)</span><span class="token punctuation">{</span>
        group<span class="token punctuation">.</span><span class="token function">threadStartFailed</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token keyword">catch</span><span class="token punctuation">(</span><span class="token class-name">Throwable</span> ignore<span class="token punctuation">)</span><span class="token punctuation">{</span>

        <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<ul>
<li>&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x65B9;&#x6CD5;start()&#xFF0C;&#x5728;&#x5B83;&#x7684;&#x65B9;&#x6CD5;&#x6CE8;&#x91CA;&#x4E2D;&#x5199;&#x5230;Causes this thread to begin execution; the Java Virtual Machine calls
the <code>run</code> method of this thread. &#x4E5F;&#x5C31;&#x662F;&#x6709;JVM&#x8C03;&#x7528;&#x6B64;&#x7EBF;&#x7A0B;&#x7684;run&#x65B9;&#x6CD5;&#xFF0C;&#x4F7F;&#x7EBF;&#x7A0B;&#x5F00;&#x59CB;&#x6267;&#x884C;&#xFF0C;&#x662F;JVM&#x7684;&#x4E00;&#x4E2A;&#x56DE;&#x8C03;&#x8FC7;&#x7A0B;</li>
<li>start0()&#x662F;&#x4E00;&#x4E2A;synchronized&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x591A;&#x6B21;&#x8C03;&#x7528;&#xFF0C;&#x5728;&#x65B9;&#x6CD5;&#x4E2D;&#x4F1A;&#x5224;&#x65AD;&#x7EBF;&#x7A0B;&#x72B6;&#x6001;</li>
<li>group.add(this)&#x662F;&#x5427;&#x4E3B;&#x7EBF;&#x7A0B;&#x52A0;&#x5165;&#x5230;&#x7EBF;&#x7A0B;&#x7EC4;&#xFF1A;THreadGroup</li>
<li>start0()&#x662F;&#x4E00;&#x4E2A;&#x672C;&#x5730;&#x65B9;&#x6CD5;&#xFF0C;&#x901A;&#x8FC7;JNI&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x6267;&#x884C;&#xFF0C;&#x8FD9;&#x4E00;&#x6B65;&#x7684;&#x64CD;&#x4F5C;&#x624D;&#x662F;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;&#x7684;&#x6838;&#x5FC3;&#x6B65;&#x9AA4;</li>
</ul>
<h3 id="start0&#x672C;&#x5730;&#x65B9;&#x6CD5;"><a name="start0&#x672C;&#x5730;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#start0&#x672C;&#x5730;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1.2. start0()&#x672C;&#x5730;&#x65B9;&#x6CD5;</h3>
<pre class="language-"><code class="lang-java"><span class="token comment">// &#x672C;&#x5730;&#x65B9;&#x6CD5;start0</span>
<span class="token keyword">private</span> <span class="token keyword">native</span> <span class="token keyword">void</span> <span class="token function">start0</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// &#x6CE8;&#x518C;&#x672C;&#x5730;&#x65B9;&#x6CD5;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Thread</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span>
    <span class="token comment">/* Make sure registerNatives is the first thing &lt;clinit&gt; does. */</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">native</span> <span class="token keyword">void</span> <span class="token function">registerNatives</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">static</span> <span class="token punctuation">{</span>
        <span class="token function">registerNatives</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>start0()&#x662F;&#x4E00;&#x4E2A;&#x672C;&#x5730;&#x65B9;&#x6CD5;&#xFF0C;&#x7528;&#x4E8E;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;</li>
<li>registerNatives() &#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x662F;&#x7528;&#x4E8E;&#x6CE8;&#x518C;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x9700;&#x8981;&#x7684;&#x4E00;&#x4E9B;&#x672C;&#x5730;&#x65B9;&#x6CD5;&#xFF0C;&#x6BD4;&#x5982;start0&#x3001;isAlive&#x3001;yield&#x3001;sleep&#x3001;interrupt0&#x7B49;</li>
</ul>
<blockquote>
<p><a href="https://gitee.com/mirrors/openjdk/blob/master/src/java.base/share/native/libjava/Thread.c" target="_blank">src/java.base/share/native/libjava/Thread.c</a></p>
</blockquote>
<pre class="language-"><code class="lang-c"><span class="token macro property"><span class="token directive-hash">#</span> <span class="token expression">JNINativeMethod&#x6E90;&#x7801;</span></span>
<span class="token keyword">static</span> JNINativeMethod methods<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token macro property"><span class="token directive-hash">#</span> <span class="token directive keyword">start</span><span class="token expression"><span class="token number">0</span>&#x65B9;&#x6CD5;&#x4F1A;&#x6267;&#x884C;<span class="token operator">&amp;</span>JVM_StartThread&#x65B9;&#x6CD5;&#xFF0C;&#x6700;&#x7EC8;&#x7531;JVM&#x5C42;&#x9762;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;</span></span>
    <span class="token punctuation">{</span><span class="token string">&quot;start0&quot;</span><span class="token punctuation">,</span>           <span class="token string">&quot;()V&quot;</span><span class="token punctuation">,</span>        <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_StartThread<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;stop0&quot;</span><span class="token punctuation">,</span>            <span class="token string">&quot;(&quot;</span> OBJ <span class="token string">&quot;)V&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_StopThread<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;isAlive&quot;</span><span class="token punctuation">,</span>          <span class="token string">&quot;()Z&quot;</span><span class="token punctuation">,</span>        <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_IsThreadAlive<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;suspend0&quot;</span><span class="token punctuation">,</span>         <span class="token string">&quot;()V&quot;</span><span class="token punctuation">,</span>        <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_SuspendThread<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;resume0&quot;</span><span class="token punctuation">,</span>          <span class="token string">&quot;()V&quot;</span><span class="token punctuation">,</span>        <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_ResumeThread<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;setPriority0&quot;</span><span class="token punctuation">,</span>     <span class="token string">&quot;(I)V&quot;</span><span class="token punctuation">,</span>       <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_SetThreadPriority<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;yield&quot;</span><span class="token punctuation">,</span>            <span class="token string">&quot;()V&quot;</span><span class="token punctuation">,</span>        <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_Yield<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;sleep&quot;</span><span class="token punctuation">,</span>            <span class="token string">&quot;(J)V&quot;</span><span class="token punctuation">,</span>       <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_Sleep<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;currentThread&quot;</span><span class="token punctuation">,</span>    <span class="token string">&quot;()&quot;</span> THD<span class="token punctuation">,</span>     <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_CurrentThread<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;interrupt0&quot;</span><span class="token punctuation">,</span>       <span class="token string">&quot;()V&quot;</span><span class="token punctuation">,</span>        <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_Interrupt<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;holdsLock&quot;</span><span class="token punctuation">,</span>        <span class="token string">&quot;(&quot;</span> OBJ <span class="token string">&quot;)Z&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_HoldsLock<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;getThreads&quot;</span><span class="token punctuation">,</span>       <span class="token string">&quot;()[&quot;</span> THD<span class="token punctuation">,</span>    <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_GetAllThreads<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;dumpThreads&quot;</span><span class="token punctuation">,</span>      <span class="token string">&quot;([&quot;</span> THD <span class="token string">&quot;)[[&quot;</span> STE<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_DumpThreads<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;setNativeName&quot;</span><span class="token punctuation">,</span>    <span class="token string">&quot;(&quot;</span> STR <span class="token string">&quot;)V&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>JVM_SetNativeThreadName<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre>
<h3 id="jvm&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;"><a name="jvm&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;" class="anchor-navigation-ex-anchor" href="#jvm&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1.3. JVM&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;</h3>
<blockquote>
<p><a href="https://gitee.com/xutingyin/jdk8-hotspot/blob/master/src/share/vm/prims/jvm.cpp" target="_blank">src/share/vm/prims/jvm.cpp &#xB7; jdk8-hotspot</a></p>
</blockquote>
<pre class="language-"><code class="lang-cpp"><span class="token comment">// JVM_StartThread&#x65B9;&#x6CD5;&#x76F8;&#x5173;&#x6E90;&#x7801;</span>
<span class="token function">JVM_ENTRY</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">,</span> <span class="token function">JVM_StartThread</span><span class="token punctuation">(</span>JNIEnv<span class="token operator">*</span> env<span class="token punctuation">,</span> jobject jthread<span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token function">JVMWrapper</span><span class="token punctuation">(</span><span class="token string">&quot;JVM_StartThread&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  JavaThread <span class="token operator">*</span>native_thread <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
  <span class="token comment">// &#x521B;&#x5EFA;&#x7EBF;&#x7A0B;</span>
  native_thread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">JavaThread</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>thread_entry<span class="token punctuation">,</span> sz<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// &#x542F;&#x52A8;&#x7EBF;&#x7A0B;</span>
  <span class="token class-name">Thread</span><span class="token double-colon punctuation">::</span><span class="token function">start</span><span class="token punctuation">(</span>native_thread<span class="token punctuation">)</span><span class="token punctuation">;</span>
JVM_END

<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">thread_entry</span><span class="token punctuation">(</span>JavaThread<span class="token operator">*</span> thread<span class="token punctuation">,</span> TRAPS<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  HandleMark <span class="token function">hm</span><span class="token punctuation">(</span>THREAD<span class="token punctuation">)</span><span class="token punctuation">;</span>
  Handle <span class="token function">obj</span><span class="token punctuation">(</span>THREAD<span class="token punctuation">,</span> thread<span class="token operator">-&gt;</span><span class="token function">threadObj</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  JavaValue <span class="token function">result</span><span class="token punctuation">(</span>T_VOID<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">JavaCalls</span><span class="token double-colon punctuation">::</span><span class="token function">call_virtual</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>result<span class="token punctuation">,</span>
                          obj<span class="token punctuation">,</span>
                          <span class="token function">KlassHandle</span><span class="token punctuation">(</span>THREAD<span class="token punctuation">,</span> <span class="token class-name">SystemDictionary</span><span class="token double-colon punctuation">::</span><span class="token function">Thread_klass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                          vmSymbols<span class="token double-colon punctuation">::</span><span class="token function">run_method_name</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                          vmSymbols<span class="token double-colon punctuation">::</span><span class="token function">void_method_signature</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                          THREAD<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>JVM_StartThread&#x76F8;&#x5173;&#x4EE3;&#x7801;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x548C;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;</li>
<li>&#x5728;&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#x5F15;&#x5165;&#x7EBF;&#x7A0B;&#x5165;&#x53E3;&#x7684;&#x65B9;&#x5F0F;thread_entry&#x4E2D;&#x5305;&#x62EC;&#x4E86;Java&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;JavaCalls::
call_virtual&#xFF0C;&#x8FD9;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4F1A;&#x6709;JVM&#x8C03;&#x7528;&#xFF0C;&#x5E76;&#x6267;&#x884C;vmSymbols::run_method_name()&#x65B9;&#x6CD5;</li>
</ul>
<blockquote>
<p><a href="https://gitee.com/mirrors/openjdk/blob/master/src/hotspot/share/classfile/vmSymbols.hpp" target="_blank">src/hotspot/share/classfile/vmSymbols.hpp</a></p>
</blockquote>
<pre class="language-"><code>#define VM_SYMBOLS_DO(template, do_alias)
template(run_method_name, &quot;run&quot;)
</code></pre><ul>
<li>&#x8FD9;&#x91CC;&#x7684;run&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x7684;Java&#x7A0B;&#x5E8F;&#x4E2D;&#x88AB;&#x8C03;&#x7528;&#x7684;run&#x65B9;&#x6CD5;</li>
</ul>
<p><strong>JavaThread</strong></p>
<blockquote>
<p><a href="https://gitee.com/xutingyin/jdk8-hotspot/blob/master/src/share/vm/runtime/thread.cpp" target="_blank">src/share/vm/runtime/thread.cpp &#xB7; jdk8-hotspot</a></p>
</blockquote>
<pre class="language-"><code class="lang-cpp"><span class="token class-name">JavaThread</span><span class="token double-colon punctuation">::</span><span class="token function">JavaThread</span><span class="token punctuation">(</span>ThreadFunction entry_point<span class="token punctuation">,</span> size_t stack_sz<span class="token punctuation">)</span> <span class="token operator">:</span>
  <span class="token function">Thread</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">if</span> <span class="token expression">INCLUDE_ALL_GCS</span></span>
  <span class="token punctuation">,</span> <span class="token function">_satb_mark_queue</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>_satb_mark_queue_set<span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token function">_dirty_card_queue</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>_dirty_card_queue_set<span class="token punctuation">)</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span> <span class="token comment">// INCLUDE_ALL_GCS</span></span>
<span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>TraceThreadEvents<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    tty<span class="token operator">-&gt;</span><span class="token function">print_cr</span><span class="token punctuation">(</span><span class="token string">&quot;creating thread %p&quot;</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token function">initialize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  _jni_attach_state <span class="token operator">=</span> _not_attaching_via_jni<span class="token punctuation">;</span>
  <span class="token function">set_entry_point</span><span class="token punctuation">(</span>entry_point<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// Create the native thread itself.</span>
  <span class="token comment">// %note runtime_23</span>
  os<span class="token double-colon punctuation">::</span>ThreadType thr_type <span class="token operator">=</span> os<span class="token double-colon punctuation">::</span>java_thread<span class="token punctuation">;</span>
  thr_type <span class="token operator">=</span> entry_point <span class="token operator">==</span> <span class="token operator">&amp;</span>compiler_thread_entry <span class="token operator">?</span> os<span class="token double-colon punctuation">::</span>compiler_thread <span class="token operator">:</span>
                                                     os<span class="token double-colon punctuation">::</span>java_thread<span class="token punctuation">;</span>
  os<span class="token double-colon punctuation">::</span><span class="token function">create_thread</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> thr_type<span class="token punctuation">,</span> stack_sz<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>ThreadFunction entry_point &#x5C31;&#x662F;&#x4E0A;&#x9762;&#x7684; thread_entry &#x65B9;&#x6CD5;</li>
<li>size_t stack_sz &#x8868;&#x793A;&#x8FDB;&#x7A0B;&#x4E2D;&#x5DF2;&#x6709;&#x7684;&#x7EBF;&#x7A0B;&#x4E2A;&#x6570;</li>
<li>&#x8FD9;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x90FD;&#x4F1A;&#x4F20;&#x7ED9; os::create_thread &#x65B9;&#x6CD5;&#xFF0C;&#x7528;&#x4E8E;&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#x4F7F;&#x7528;</li>
</ul>
<p><strong>os::create_thread</strong></p>
<p>jvm&#x7684;OS&#x670D;&#x52A1;&#x5B9E;&#x73B0;&#xFF0C;&#x6709;Linux&#x3001;windows&#x7B49;&#xFF0C;&#x90FD;&#x4F1A;&#x5B9E;&#x73B0;&#x7EBF;&#x7A0B;&#x7684;&#x521B;&#x5EFA;&#x903B;&#x8F91;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;&#x9002;&#x914D;&#x5668;&#x6A21;&#x5F0F;</p>
<blockquote>
<p><a href="https://gitee.com/xutingyin/jdk8-hotspot/blob/master/src/os/windows/vm/os_windows.cpp" target="_blank">src/os/windows/vm/os_windows.cpp &#xB7; jdk8-hotspot</a></p>
<p><a href="https://gitee.com/xutingyin/jdk8-hotspot/blob/master/src/os/linux/vm/os_linux.cpp" target="_blank">src/os/linux/vm/os_linux.cpp &#xB7; jdk8-hotspot</a></p>
</blockquote>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">bool</span> os<span class="token double-colon punctuation">::</span><span class="token function">create_thread</span><span class="token punctuation">(</span>Thread<span class="token operator">*</span> thread<span class="token punctuation">,</span> ThreadType thr_type<span class="token punctuation">,</span> size_t stack_size<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">assert</span><span class="token punctuation">(</span>thread<span class="token operator">-&gt;</span><span class="token function">osthread</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token string">&quot;caller responsible&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// Allocate the OSThread object</span>
  OSThread<span class="token operator">*</span> osthread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">OSThread</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// set the correct thread state</span>
  osthread<span class="token operator">-&gt;</span><span class="token function">set_thread_type</span><span class="token punctuation">(</span>thr_type<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// Initial state is ALLOCATED but not INITIALIZED</span>
  osthread<span class="token operator">-&gt;</span><span class="token function">set_state</span><span class="token punctuation">(</span>ALLOCATED<span class="token punctuation">)</span><span class="token punctuation">;</span>

  thread<span class="token operator">-&gt;</span><span class="token function">set_osthread</span><span class="token punctuation">(</span>osthread<span class="token punctuation">)</span><span class="token punctuation">;</span>

  pthread_t tid<span class="token punctuation">;</span>
  <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">pthread_create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>tid<span class="token punctuation">,</span> <span class="token operator">&amp;</span>attr<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> <span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">)</span> java_start<span class="token punctuation">,</span> thread<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>osthread-&gt;set_state(ALLOCATED); &#x521D;&#x59CB;&#x5316;&#x5DF2;&#x5206;&#x914D;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x4F46;&#x6B64;&#x65F6;&#x5E76;&#x6CA1;&#x6709;&#x521D;&#x59CB;&#x5316;</li>
<li>pthread_create &#x662F;&#x7C7B;Unix&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#xFF08;Unix&#x3001;Linux&#x3001;Mac OS X &#x7B49;&#xFF09;&#x7684;&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#x7684;&#x51FD;&#x6570;</li>
<li>java_start &#x91CD;&#x70B9;&#x5173;&#x6CE8;&#x7C7B;&#xFF0C;&#x662F;&#x5B9E;&#x9645;&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#x7684;&#x65B9;&#x6CD5;</li>
</ul>
<p><strong>java_start</strong></p>
<blockquote>
<p><a href="https://gitee.com/xutingyin/jdk8-hotspot/blob/master/src/os/linux/vm/os_linux.cpp" target="_blank">src/os/linux/vm/os_linux.cpp &#xB7; jdk8-hotspot</a></p>
</blockquote>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token operator">*</span><span class="token function">java_start</span><span class="token punctuation">(</span>Thread <span class="token operator">*</span>thread<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// &#x7EBF;&#x7A0B;ID</span>
  <span class="token keyword">int</span> pid <span class="token operator">=</span> os<span class="token double-colon punctuation">::</span><span class="token function">current_process_id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// &#x8BBE;&#x7F6E;&#x7EBF;&#x7A0B;</span>
  <span class="token class-name">ThreadLocalStorage</span><span class="token double-colon punctuation">::</span><span class="token function">set_thread</span><span class="token punctuation">(</span>thread<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// &#x8BBE;&#x7F6E;&#x53BF;&#x57CE;&#x72B6;&#x6001;&#xFF0C;INITIALIZED&#x521D;&#x59CB;&#x5316;&#x5B8C;&#x6210;</span>
  osthread<span class="token operator">-&gt;</span><span class="token function">set_state</span><span class="token punctuation">(</span>INITIALIZED<span class="token punctuation">)</span><span class="token punctuation">;</span>
  sync<span class="token operator">-&gt;</span><span class="token function">notify_all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// &#x5FAA;&#x73AF;&#xFF0C;&#x521D;&#x59CB;&#x5316;&#x72B6;&#x6001;&#xFF0C;&#x5219;&#x4E00;&#x81F4;&#x7B49;&#x5F85;wait</span>
  <span class="token keyword">while</span> <span class="token punctuation">(</span>osthread<span class="token operator">-&gt;</span><span class="token function">get_state</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> INITIALIZED<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    sync<span class="token operator">-&gt;</span><span class="token function">wait</span><span class="token punctuation">(</span>Mutex<span class="token double-colon punctuation">::</span>_no_safepoint_check_flag<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// &#x7B49;&#x5F85;&#x5524;&#x9192;&#x540E;&#xFF0C;&#x6267;&#x884C; run &#x65B9;&#x6CD5;</span>
  thread<span class="token operator">-&gt;</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>JVM&#x8BBE;&#x7F6E;&#x7EBF;&#x7A0B;&#x72B6;&#x6001;&#xFF0C;INITIALIZED&#x521D;&#x59CB;&#x5316;&#x5B8C;&#x6210;</li>
<li>sync-&gt;notify_all(); &#x5524;&#x9192;&#x6240;&#x6709;&#x7EBF;&#x7A0B;</li>
<li>osthread-&gt;get_state() == INITIALIZED while &#x5FAA;&#x73AF;&#x7B49;&#x5F85;</li>
<li>thread-&gt;run(); &#x662F;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x5524;&#x9192;&#x540E;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x72B6;&#x6001;&#x53D8;&#x66F4;&#x540E;&#xFF0C;&#x624D;&#x80FD;&#x6267;&#x884C;&#x5230;</li>
</ul>
<h3 id="jvm&#x542F;&#x52A8;&#x7EBF;&#x7A0B;"><a name="jvm&#x542F;&#x52A8;&#x7EBF;&#x7A0B;" class="anchor-navigation-ex-anchor" href="#jvm&#x542F;&#x52A8;&#x7EBF;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1.4. JVM&#x542F;&#x52A8;&#x7EBF;&#x7A0B;</h3>
<pre class="language-"><code class="lang-cpp"><span class="token function">JVM_ENTRY</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">,</span> <span class="token function">JVM_StartThread</span><span class="token punctuation">(</span>JNIEnv<span class="token operator">*</span> env<span class="token punctuation">,</span> jobject jthread<span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token function">JVMWrapper</span><span class="token punctuation">(</span><span class="token string">&quot;JVM_StartThread&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  JavaThread <span class="token operator">*</span>native_thread <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>

  <span class="token comment">// &#x521B;&#x5EFA;&#x7EBF;&#x7A0B;</span>
  native_thread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">JavaThread</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>thread_entry<span class="token punctuation">,</span> sz<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// &#x542F;&#x52A8;&#x7EBF;&#x7A0B;</span>
  <span class="token class-name">Thread</span><span class="token double-colon punctuation">::</span><span class="token function">start</span><span class="token punctuation">(</span>native_thread<span class="token punctuation">)</span><span class="token punctuation">;</span>

JVM_END
</code></pre>
<ul>
<li>JVM_StartThread &#x4E2D;&#x6709;&#x4E24;&#x6B65;&#xFF0C;&#x521B;&#x5EFA;&#xFF08;new JavaThread&#xFF09;&#x3001;&#x542F;&#x52A8;&#xFF08;Thread::start&#xFF09;</li>
</ul>
<p><strong>Thread::start</strong></p>
<blockquote>
<p><a href="https://gitee.com/xutingyin/jdk8-hotspot/blob/master/src/share/vm/runtime/thread.cpp" target="_blank">src/share/vm/runtime/thread.cpp &#xB7; jdk8-hotspot</a></p>
</blockquote>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">Thread</span><span class="token double-colon punctuation">::</span><span class="token function">start</span><span class="token punctuation">(</span>Thread<span class="token operator">*</span> thread<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">trace</span><span class="token punctuation">(</span><span class="token string">&quot;start&quot;</span><span class="token punctuation">,</span> thread<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>DisableStartThread<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>thread<span class="token operator">-&gt;</span><span class="token function">is_Java_thread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      java_lang_Thread<span class="token double-colon punctuation">::</span><span class="token function">set_thread_status</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>JavaThread<span class="token operator">*</span><span class="token punctuation">)</span>thread<span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">threadObj</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                          java_lang_Thread<span class="token double-colon punctuation">::</span>RUNNABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x4E0D;&#x540C;&#x7684; os &#x4F1A;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x542F;&#x52A8;&#x4EE3;&#x7406;&#x903B;&#x8F91;</span>
    os<span class="token double-colon punctuation">::</span><span class="token function">start_thread</span><span class="token punctuation">(</span>thread<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7981;&#x7528;&#x7EBF;&#x7A0B; DisableStartThread &#x5E76;&#x4E14;&#x662F; Java &#x7EBF;&#x7A0B; thread-&gt;is_Java_thread()&#xFF0C;&#x90A3;&#x4E48;&#x8BBE;&#x7F6E;&#x7EBF;&#x7A0B;&#x72B6;&#x6001;&#x4E3A; RUNNABLE</li>
<li>os::start_thread(thread)&#xFF0C;&#x8C03;&#x7528;&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x540C;&#x7684; OS &#x4F1A;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x542F;&#x52A8;&#x4EE3;&#x7801;&#x903B;&#x8F91;</li>
</ul>
<p><strong>os::start_thread(thread)</strong></p>
<blockquote>
<p><a href="https://gitee.com/xutingyin/jdk8-hotspot/blob/master/src/share/vm/runtime/os.cpp" target="_blank">src/share/vm/runtime/os.cpp &#xB7; jdk8-hotspot</a></p>
</blockquote>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> os<span class="token double-colon punctuation">::</span><span class="token function">start_thread</span><span class="token punctuation">(</span>Thread<span class="token operator">*</span> thread<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// guard suspend/resume</span>
  MutexLockerEx <span class="token function">ml</span><span class="token punctuation">(</span>thread<span class="token operator">-&gt;</span><span class="token function">SR_lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> Mutex<span class="token double-colon punctuation">::</span>_no_safepoint_check_flag<span class="token punctuation">)</span><span class="token punctuation">;</span>
  OSThread<span class="token operator">*</span> osthread <span class="token operator">=</span> thread<span class="token operator">-&gt;</span><span class="token function">osthread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  osthread<span class="token operator">-&gt;</span><span class="token function">set_state</span><span class="token punctuation">(</span>RUNNABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">pd_start_thread</span><span class="token punctuation">(</span>thread<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>osthread-&gt;set_state(RUNNABLE) &#x8BBE;&#x7F6E;&#x54B8;&#x4EA8;&#x72B6;&#x6001; RUNNABLE</li>
<li>pd_start_thread(thread) &#x542F;&#x52A8;&#x7EBF;&#x7A0B;&#xFF0C;&#x8FD9;&#x4E2A;&#x662F;&#x7531;&#x5404;&#x4E2A; OS &#x5B9E;&#x73B0;&#x7C7B;&#xFF0C;&#x5B9E;&#x73B0;&#x5404;&#x81EA;&#x7CFB;&#x7EDF;&#x7684;&#x542F;&#x52A8;&#x65B9;&#x6CD5;</li>
</ul>
<p><strong>pd_start_thread(thread)</strong></p>
<blockquote>
<p><a href="https://gitee.com/xutingyin/jdk8-hotspot/blob/master/src/os/linux/vm/os_linux.cpp" target="_blank">src/os/linux/vm/os_linux.cpp &#xB7; jdk8-hotspot</a></p>
</blockquote>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> os<span class="token double-colon punctuation">::</span><span class="token function">pd_start_thread</span><span class="token punctuation">(</span>Thread<span class="token operator">*</span> thread<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  OSThread <span class="token operator">*</span> osthread <span class="token operator">=</span> thread<span class="token operator">-&gt;</span><span class="token function">osthread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">assert</span><span class="token punctuation">(</span>osthread<span class="token operator">-&gt;</span><span class="token function">get_state</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> INITIALIZED<span class="token punctuation">,</span> <span class="token string">&quot;just checking&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  Monitor<span class="token operator">*</span> sync_with_child <span class="token operator">=</span> osthread<span class="token operator">-&gt;</span><span class="token function">startThread_lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  MutexLockerEx <span class="token function">ml</span><span class="token punctuation">(</span>sync_with_child<span class="token punctuation">,</span> Mutex<span class="token double-colon punctuation">::</span>_no_safepoint_check_flag<span class="token punctuation">)</span><span class="token punctuation">;</span>
  sync_with_child<span class="token operator">-&gt;</span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>&#x8FD9;&#x90E8;&#x5206;&#x6700;&#x5173;&#x952E;&#x7684;&#x4EE3;&#x7801;&#x662F; notify()&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x5524;&#x9192;&#x7EBF;&#x7A0B;</li>
<li>&#x7EBF;&#x7A0B;&#x5524;&#x9192;&#x540E;&#xFF0C;java_start &#x4E2D;&#x7684; thread-&gt;run() &#x5C31;&#x53EF;&#x4EE5;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x4E86;</li>
</ul>
<h3 id="jvm&#x56DE;&#x8C03;"><a name="jvm&#x56DE;&#x8C03;" class="anchor-navigation-ex-anchor" href="#jvm&#x56DE;&#x8C03;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1.5. JVM&#x56DE;&#x8C03;</h3>
<p><strong>thread -&gt; run()[JavaThread::run()]</strong></p>
<blockquote>
<p><a href="https://gitee.com/xutingyin/jdk8-hotspot/blob/master/src/share/vm/runtime/thread.cpp" target="_blank">src/share/vm/runtime/thread.cpp &#xB7; jdk8-hotspot</a></p>
</blockquote>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">JavaThread</span><span class="token double-colon punctuation">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// ... &#x521D;&#x59CB;&#x5316;&#x64CD;&#x4F5C;</span>
  <span class="token function">thread_main_inner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>os_linux.cpp &#x7C7B;&#x4E2D;&#x7684; java_start &#x91CC;&#x7684; thread-&gt;run()&#xFF0C;&#x6700;&#x7EC8;&#x8C03;&#x7528;&#x7684;&#x5C31;&#x662F; thread.cpp &#x7684; JavaThread.run() &#x65B9;&#x6CD5;</li>
</ul>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token class-name">JavaThread</span><span class="token double-colon punctuation">::</span><span class="token function">thread_main_inner</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">assert</span><span class="token punctuation">(</span><span class="token class-name">JavaThread</span><span class="token double-colon punctuation">::</span><span class="token function">current</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">this</span><span class="token punctuation">,</span> <span class="token string">&quot;sanity check&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">assert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token operator">-&gt;</span><span class="token function">threadObj</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token string">&quot;just checking&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword">this</span><span class="token operator">-&gt;</span><span class="token function">has_pending_exception</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span>
      <span class="token operator">!</span>java_lang_Thread<span class="token double-colon punctuation">::</span><span class="token function">is_stillborn</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token operator">-&gt;</span><span class="token function">threadObj</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">{</span>
      ResourceMark <span class="token function">rm</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">this</span><span class="token operator">-&gt;</span><span class="token function">set_native_thread_name</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token operator">-&gt;</span><span class="token function">get_thread_name</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    HandleMark <span class="token function">hm</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token operator">-&gt;</span><span class="token function">entry_point</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token function">DTRACE_THREAD_PROBE</span><span class="token punctuation">(</span>stop<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">this</span><span class="token operator">-&gt;</span><span class="token function">exit</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">delete</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>&#x4EE3;&#x7801;&#x4E2D;&#x8BBE;&#x7F6E;&#x53BF;&#x57CE;&#x540D;&#x79F0; this-&gt;set_native_thread_name(this-&gt;get_thread_name())&#xFF0C;&#x5B9E;&#x9645;&#x8C03;&#x7528;&#x7684;&#x5C31;&#x662F;JVM&#x521B;&#x5EFA;&#x4E2D;&#x7684; thread_entry &#x65B9;&#x6CD5;</li>
<li>thread_entry &#x65B9;&#x6CD5;&#x6700;&#x7EC8;&#x4F1A;&#x8C03;&#x7528;&#x5230; JavaCalls::call_virtual &#x91CC;&#x7684; vmSymbols::run_method_name() &#x4E5F;&#x5C31;&#x662F; run &#x65B9;&#x6CD5;&#xFF0C;&#x81F3;&#x6B64;&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x5B8C;&#x6210;</li>
</ul>
<h3 id="&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x603B;&#x7ED3;"><a name="&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x603B;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x603B;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1.6. &#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x603B;&#x7ED3;</h3>
<ul>
<li>&#x7EBF;&#x7A0B;&#x7684;&#x542F;&#x52A8;&#x8FC7;&#x7A0B;&#x6D89;&#x53CA;&#x5230;&#x4E86; JVM &#x7684;&#x53C2;&#x4E0E;</li>
<li>&#x6574;&#x4E2A;&#x6E90;&#x7801;&#x5206;&#x6790;&#x5305;&#x62EC;&#xFF1A;<ul>
<li>Java&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#x548C;&#x542F;&#x52A8;</li>
<li>&#x8C03;&#x7528;&#x672C;&#x5730;&#x65B9;&#x6CD5;</li>
<li>start0()</li>
<li>JVM&#x4E2D;&#x7684;JVM_StartThread&#x7684;&#x521B;&#x5EFA;&#x548C;&#x542F;&#x52A8;</li>
<li>&#x8BBE;&#x7F6E;&#x53BF;&#x57CE;&#x72B6;&#x6001;&#x7B49;&#x5F85;&#x88AB;&#x5524;&#x9192;</li>
<li>&#x6839;&#x636E;&#x4E0D;&#x540C;&#x7684;OS&#x542F;&#x52A8;&#x7EBF;&#x7A0B;&#x5E76;&#x5524;&#x9192;</li>
<li>&#x6700;&#x540E;&#x56DE;&#x8C03;run()&#x65B9;&#x6CD5;&#x542F;&#x52A8;Java&#x7EBF;&#x7A0B;</li>
</ul>
</li>
</ul>
<h3 id="&#x65B9;&#x6CD5;&#x5F15;&#x7528;"><a name="&#x65B9;&#x6CD5;&#x5F15;&#x7528;" class="anchor-navigation-ex-anchor" href="#&#x65B9;&#x6CD5;&#x5F15;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1.7. &#x65B9;&#x6CD5;&#x5F15;&#x7528;</h3>
<table>
<thead>
<tr>
<th>CLASS</th>
<th>USE</th>
</tr>
</thead>
<tbody>
<tr>
<td>static</td>
<td>ClassName::method</td>
</tr>
<tr>
<td>instance</td>
<td>instance::method</td>
</tr>
<tr>
<td>class</td>
<td>ClassName::method</td>
</tr>
<tr>
<td>constructor</td>
<td>ClassName::new</td>
</tr>
</tbody>
</table>
<p><a href="https://www.cnblogs.com/qdhxhz/category/1266116.html" target="_blank">https://www.cnblogs.com/qdhxhz/category/1266116.html</a></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="java.html" class="navigation navigation-prev " aria-label="Previous page: Java基础知识">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="java-jvm.html" class="navigation navigation-next " aria-label="Next page: Java_JVM">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Java_Summary","level":"1.3.1","depth":2,"next":{"title":"Java_JVM","level":"1.3.2","depth":2,"path":"markdown/java/java-jvm.md","ref":"markdown/java/java-jvm.md","articles":[]},"previous":{"title":"Java基础知识","level":"1.3","depth":1,"path":"markdown/java/java.md","ref":"markdown/java/java.md","articles":[{"title":"Java_Summary","level":"1.3.1","depth":2,"path":"markdown/java/java-summary.md","ref":"markdown/java/java-summary.md","articles":[]},{"title":"Java_JVM","level":"1.3.2","depth":2,"path":"markdown/java/java-jvm.md","ref":"markdown/java/java-jvm.md","articles":[]},{"title":"effectiveJava","level":"1.3.3","depth":2,"path":"markdown/java/effectiveJava.md","ref":"markdown/java/effectiveJava.md","articles":[]},{"title":"设计模式","level":"1.3.4","depth":2,"path":"markdown/java/design-pattern.md","ref":"markdown/java/design-pattern.md","articles":[]},{"title":"JavaTest","level":"1.3.5","depth":2,"path":"markdown/java/JavaTest.md","ref":"markdown/java/JavaTest.md","articles":[]},{"title":"MyFrame","level":"1.3.6","depth":2,"path":"markdown/java/myframe.md","ref":"markdown/java/myframe.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["theme-default","prism","-highlight","search-pro","-search","-lunr","expandable-chapters-small","splitter","-sharing","anchor-navigation-ex","code"],"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"},"pluginsConfig":{"prism":{},"search-pro":{},"expandable-chapters-small":{},"splitter":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"code":{"copyButtons":true},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"showLevel":false,"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}},"theme":"default","author":"chenjiebin","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pluginConfig":{"prism":{"css":["prismjs/themes/prism-solarizedlight.css"]},"anchor-navigation-ex":{"showLevel":true},"theme-default":{"toggleMenu":false}},"variables":{},"title":"GitBook笔记","gitbook":"*","description":"学习笔记，包含前后端基础知识及框架的学习笔记"},"file":{"path":"markdown/java/java-summary.md","mtime":"2023-11-19T13:43:04.121Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2024-02-28T09:32:04.838Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

